Open In App

Python | Find fibonacci series upto n using lambda

Last Updated : 25 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

Fn = Fn-1 + Fn-2  with seed values   F0 = 0 and F1 = 1.

Find the series of fibonacci numbers using lambda function. Code #1 : By using lambda and reduce method 

Python3




from functools import reduce
 
fib = lambda n: reduce(lambda x, _: x+[x[-1]+x[-2]],
                                 range(n-2), [0, 1])
 
print(fib(5))


Output:

[0, 1, 1, 2, 3]

Explanation : The list taking first two parameters is 0 and 1, and add like x[-1] i.e 0 and x[-2] i.e 1 and append to variable x. There is a type conversion to list and due to reduce() method, the same function calls and due to range function this time parameter changes, then add this to previous result and again store it to list.   Code #2 : By using lambda and map function 

Python3




def fibonacci(count):
    fib_list = [0, 1]
 
    any(map(lambda _: fib_list.append(sum(fib_list[-2:])),
                                         range(2, count)))
 
    return fib_list[:count]
 
print(fibonacci(10))


Output:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Explanation : We are taking the list fib_list which already has 0 and 1. Then in the next iteration, this will be used as input and result of their sum will append to the list.

Time complexity: O(n), where n is the length of fib_list.
Auxiliary Space: O(n), where n is the length of fib_list.



Previous Article
Next Article

Similar Reads

Count of numbers whose difference with Fibonacci count upto them is atleast K
Prerequisites: Binary SearchGiven two positive integers N and K, the task is to count all the numbers that satisfy the following conditions: If the number is num, num ? N.abs(num - count) ? K where count is the count of fibonacci numbers upto num. Examples: Input: N = 10, K = 3 Output: 2 Explanation: 9 and 10 are the valid numbers which satisfy the
9 min read
Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space. Examples: Input : [1, 2, 3, 2, 3, 1, 3] Output : 3 We have existing solution for this problem please refer Find the Number Occurring Odd Number of Times link. we will solv
1 min read
Check if a M-th fibonacci number divides N-th fibonacci number
Given two numbers M and N, the task is to check if the M-th and N-th Fibonacci numbers perfectly divide each other or not.Examples: Input: M = 3, N = 6 Output: Yes F(3) = 2, F(6) = 8 and F(6) % F(3) = 0 Input: M = 2, N = 9 Output: Yes A naive approach will be to find the N-th and M-th Fibonacci numbers and check if they are perfectly divisible or n
8 min read
Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
Given an array arr[] containing N elements, the task is to check if the sum of Fibonacci elements of the array is a fibonacci number or not.Examples: Input: arr[] = {2, 3, 7, 11} Output: Yes Explanation: As there are two Fibonacci numbers in the array i.e. 2 and 3. So, the sum of Fibonacci numbers is 2 + 3 = 5 and 5 is also a Fibonacci number. Inpu
7 min read
Fibonacci Series Program In Python Using Iterative Method
Fibonacci series is a series where each number is the sum of its two previous numbers. In this article, we are going to generate Fibonacci series in Python using Iterative methods. We will be covering both the loops i.e. for loop and while loop. In this article, we will be covering all the concepts related to the topic with clear and concise exampl
4 min read
Fibonacci Series In Python Using For Loop'
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. This series is not only intriguing due to its inherent mathematical properties but also widely used in various fields such as mathematics, compu
3 min read
Find n terms of Fibonacci type series with given first two terms
Given first two numbers of series, find n terms of series with these two numbers. The given series follows the same concept as Fibonacci series, i.e., n-th term is sum of (n-1)-th and (n-2)-th terms. Examples: Input: first = 5, sec = 8, n = 5 Output: 5, 8, 13, 21, 34 Input: first = 2, sec = 4, n = 5 Output: 2, 4, 6, 10, 16 Approach: The approach is
7 min read
Find the Nth element of the modified Fibonacci series
Given two integers A and B which are the first two terms of the series and another integer N. The task is to find the Nth number using Fibonacci rule i.e. fib(i) = fib(i - 1) + fib(i - 2)Example: Input: A = 2, B = 3, N = 4 Output: 8 The series will be 2, 3, 5, 8, 13, 21, ... And the 4th element is 8.Input: A = 5, B = 7, N = 10 Output: 343 Approach:
5 min read
Python - Lambda function to find the smaller value between two elements
The lambda function is an anonymous function. It can have any number of arguments but it can only have one expression. Syntax lambda arguments : expression In this article, we will learn how to find the smaller value between two elements using the Lambda function. Example: Input : 2 5 Output : 2 Input : 7 5 Output : 5Method 1: Using lambda and min(
2 min read
Print Fibonacci Series in reverse order using Recursion
Given an integer N, the task is to print the first N terms of the Fibonacci series in reverse order using Recursion. Examples: Input: N = 5Output: 3 2 1 1 0Explanation: First five terms are - 0 1 1 2 3. Input: N = 10Output: 34 21 13 8 5 3 2 1 1 0 Approach: The idea is to use recursion in a way that keeps calling the same function again till N is gr
3 min read
three90RightbarBannerImg