Given a number N, find the first N Fibonacci numbers. The first two number of the series are 1 and 1.
Example 1:
Input:
N = 5
Output: 1 1 2 3 5
Example 2:
Input:
N = 7
Output: 1 1 2 3 5 8 13
Your Task:
Your task is to complete printFibb() which takes single argument N and returns a list of first N Fibonacci numbers.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Note: This space is used to store and return the answer for printing purpose.
Constraints:
1<= N <=84

Source: geeksforgeeks | Difficulty: EASY
Tags: Dynamic programming

Mathematics

Starter code: #User function Template for python3



class Solution:

    #Function to return list containing first n fibonacci numbers.

    def printFibb(self,n):

        # your code here

Solutions:
class Solution:

	def printFibb(self, n):
		result = [0] * n
		f1 = 1
		f2 = 1
		if n == 1:
			result[0] = 1
		else:
			result[0] = 1
			result[1] = 1
			i = 2
			while i < n:
				f = f1 + f2
				f2 = f1
				f1 = f
				result[i] = f
				i += 1
		return result


class Solution:

	def printFibb(self, n):
		fib = [1, 1]
		for i in range(2, n):
			fib.append(fib[i - 1] + fib[i - 2])
		return fib[:n]


class Solution:

	def printFibb(self, n):
		a = 0
		b = 1
		l = [1]
		c = 0
		for i in range(1, n):
			c = a + b
			l.append(c)
			a = b
			b = c
		return l


class Solution:

	def printFibb(self, n):

		def fibonacci(l):
			if l <= 0:
				return []
			elif l == 1:
				return [0]
			elif l == 2:
				return [0, 1]
			else:
				fib_sequence = fibonacci(l - 1)
				fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
				return fib_sequence
		result = fibonacci(n + 1)
		return result[1:]


class Solution:

	def printFibb(self, n):
		n1 = 0
		n2 = 1
		for i in range(n):
			print(n2, end=' ')
			s = n1 + n2
			n1 = n2
			n2 = s
		return []


class Solution:

	def printFibb(self, n):
		res = []
		if n == 1:
			res = [1]
		elif n == 2:
			res = [1, 1]
		else:
			res = [1, 1]
			for i in range(2, n):
				res.append(res[-2] + res[-1])
		return res


class Solution:

	def printFibb(self, n):
		arr = [1, 1]
		for i in range(2, n):
			arr.append(arr[i - 1] + arr[i - 2])
		if n == 1:
			return [1]
		else:
			return arr


class Solution:

	def printFibb(self, n):
		if n == 1:
			return [1]
		dp = [1, 1]
		for i in range(2, n):
			dp.append(dp[len(dp) - 1] + dp[len(dp) - 2])
		return dp


class Solution:

	def printFibb(self, n):
		fib_num = [1, 1]
		for i in range(2, n):
			n3 = fib_num[i - 1] + fib_num[i - 2]
			fib_num.append(n3)
		return fib_num[:n]


class Solution:

	def printFibb(self, n):
		if n == 1:
			return [1]
		v = [0] * n
		v[0] = v[1] = 1
		for i in range(2, n):
			v[i] = v[i - 1] + v[i - 2]
		return v


class Solution:

	def printFibb(self, n):
		a = 1
		b = 1
		ans = []
		ans.append(a)
		ans.append(b)
		if n == 1:
			return [1]
		for i in range(n - 2):
			nxt = a + b
			ans.append(nxt)
			a = b
			b = nxt
		return ans


class Solution:

	def printFibb(self, n):
		fib_numbers = [0] * n
		for i in range(n):
			if i < 2:
				fib_numbers[i] = 1
			else:
				fib_numbers[i] = fib_numbers[i - 1] + fib_numbers[i - 2]
		return fib_numbers


class Solution:

	def printFibb(self, n):
		res = [0, 1]
		for i in range(n - 1):
			res.append(res[-1] + res[-2])
		return res[1:]


class Solution:

	def printFibb(self, n):
		y = []
		if n == 1:
			return [1]
		elif n == 2:
			return [1, 1]
		elif n > 2:
			y.append(1)
			y.append(1)
			for i in range(2, n):
				y.append(y[i - 1] + y[i - 2])
		return y


class Solution:

	def printFibb(self, n):
		ans = []
		f = 0
		s = 1
		ans.append(s)
		for i in range(1, n):
			t = f + s
			ans.append(t)
			f = s
			s = t
		return ans


class Solution:

	def printFibb(self, n):
		out = [1, 1]
		if n == 1:
			return [1]
		if n == 2:
			return [1, 1]
		for i in range(n - 2):
			out.append(out[-1] + out[-2])
		return out


class Solution:

	def printFibb(self, n):
		if n == 1:
			return [1]
		output = [1, 1]
		for _ in range(n - 2):
			output.append(output[-1] + output[-2])
		return output


class Solution:

	def printFibb(self, n):
		ans = []
		x = int(0)
		y = int(1)
		while n > 0:
			(x, y) = (y, x + y)
			ans.append(x)
			n -= 1
		return ans


class Solution:

	def printFibb(self, n):
		arr = [0] * n
		if n == 1:
			return [1]
		elif n >= 2:
			arr[0] = 1
			arr[1] = 1
			for i in range(2, n):
				arr[i] = arr[i - 1] + arr[i - 2]
		return arr


class Solution:

	def printFibb(self, n):
		a = 0
		b = 1
		l1 = [1]
		l = [1, 1]
		c = a + b
		for i in range(2, n):
			a = b
			b = c
			c = a + b
			l.append(c)
		if n == 1:
			return l1
		else:
			return l


class Solution:

	def printFibb(self, n):
		if n == 1:
			return [1]
		elif n == 0:
			return []
		ans = [1, 1]
		i = 0
		j = 1
		while len(ans) < n:
			ans.append(ans[i] + ans[j])
			i = i + 1
			j = j + 1
		return ans


class Solution:

	def printFibb(self, n):
		if n == 1:
			return [1]
		if n == 2:
			return [1, 1]
		x = 1
		y = 1
		arr = [1, 1]
		while n > 2:
			arr.append(x + y)
			y = x + y
			x = y - x
			n -= 1
		return arr


class Solution:

	def printFibb(self, n):
		if n == 1:
			return [1]
		if n == 2:
			return [1, 1]
		else:
			ans = [0 for i in range(n)]
			ans[0] = 1
			ans[1] = 1
			for i in range(2, n):
				ans[i] = ans[i - 1] + ans[i - 2]
		return ans

	def fibb(self, n):
		if n <= 0:
			return 0
		if n == 1 or n == 2:
			return 1
		return self.fibb(n - 1) + self.fibb(n - 2)


class Solution:

	def fib(self, n, ans):
		if n <= 1:
			return n
		if n in ans:
			return ans[n]
		s = self.fib(n - 1, ans) + self.fib(n - 2, ans)
		ans[n] = s
		return s

	def printFibb(self, n):
		ans = []
		memo = {}
		for i in range(n):
			ans.append(self.fib(i + 1, memo))
		return ans


class Solution:

	def printFibb(self, n):
		fib = [0] * (n + 1)
		if n == 1:
			return [1]
		elif n == 2:
			return [1, 1]
		else:
			fib[1] = 1
			fib[2] = 1
			for i in range(3, n + 1):
				fib[i] = fib[i - 1] + fib[i - 2]
			return fib[1:]


class Solution:

	def printFibb(self, n):
		lis = [1]

		def fib(n, a=0, b=1):
			if n == 1:
				return
			c = a + b
			lis.append(c)
			fib(n - 1, b, c)
		fib(n)
		return lis


class Solution:

	def printFibb(self, n):
		if n == 0:
			return []
		elif n == 1:
			return [1]
		else:
			fib = [1, 1]
			while len(fib) < n:
				fib.append(fib[-1] + fib[-2])
			return fib


class Solution:

	def printFibb(self, n):
		lst = []
		first = 1
		sec = 1
		lst.append(first)
		if n >= 2:
			lst.append(sec)
		for i in range(2, n):
			lst.append(lst[i - 1] + lst[i - 2])
		return lst


class Solution:

	def printFibb(self, n):
		l = []
		if n == 1:
			l.append(1)
		elif n == 2:
			l.append(1)
			l.append(1)
		else:
			l.append(1)
			l.append(1)
			a = b = 1
			for i in range(n - 2):
				z = a + b
				l.append(z)
				a = b
				b = z
		return l


class Solution:

	def printFibb(self, n):
		lst = []
		i = 0
		j = 1
		if n >= 1:
			lst.append(1)
		while n > 1:
			temp = i + j
			lst.append(temp)
			i = j
			j = temp
			n -= 1
		return lst


class Solution:

	def printFibb(self, n):
		l = []
		(f1, f2) = (1, 1)
		for i in range(n):
			l.append(f1)
			f3 = f1 + f2
			f1 = f2
			f2 = f3
		return l


class Solution:

	def printFibb(self, n):
		pr = [1]
		if n == 1:
			return pr
		sr = [1, 1]
		t = len(sr)
		f = 1
		s = 1
		for i in range(n - 2):
			c = f + s
			sr.append(c)
			f = s
			s = c
		return sr


class Solution:

	def printFibb(self, n):
		fibo = [1, 1]
		if n <= 2:
			return fibo[:n]
		for i in range(2, n):
			sum = 0
			sum = fibo[i - 2] + fibo[i - 1]
			fibo.append(sum)
		return fibo


class Solution:

	def printFibb(self, n):
		prev = 0
		next = 1
		ans = []
		ans.append(1)
		for i in range(n - 1):
			data = prev + next
			ans.append(data)
			prev = next
			next = data
		return ans


class Solution:

	def recursion(self, n, ans, prev, next):
		if n == 1 or n == 0:
			return ans
		data = prev + next
		ans.append(data)
		prev = next
		next = data
		self.recursion(n - 1, ans, prev, next)
		return ans

	def printFibb(self, n):
		ans = []
		prev = 0
		next = 1
		ans.append(1)
		self.recursion(n, ans, prev, next)
		return ans


class Solution:

	def printFibb(self, n):
		f = 1
		s = 1
		l = []
		for i in range(n):
			l.append(f)
			(f, s) = (s, f + s)
		return l


class Solution:

	def printFibb(self, n):
		a = [1, 1]
		if n == 1:
			a.pop()
			return a
		i = 0
		while i < n - 2:
			item = a[-2] + a[-1]
			a.append(item)
			i += 1
		return a


class Solution:

	def printFibb(self, n):
		if n is 1:
			return [1]
		res = [1, 1]
		for i in range(2, n):
			res.append(res[i - 1] + res[i - 2])
		return res


class Solution:
	fibo = [0 if i == 0 else 1 for i in range(85)]

	def genFibo(self, n):
		for i in range(2, n):
			self.fibo[i] = self.fibo[i - 1] + self.fibo[i - 2]

	def printFibb(self, n):
		if self.fibo[84] == 1:
			self.genFibo(85)
		return self.fibo[1:n + 1]


class Solution:

	def printFibb(self, n):

		def solve(i, l, s):
			if i > n:
				return []
			small = solve(i + 1, s, s + l)
			small.append(l)
			return small
		ans = solve(0, 0, 1)
		ans.pop()
		return ans[::-1]


class Solution:

	def printFibb(self, n):
		fib = []
		i = 3
		if n == 1:
			fib.append(1)
		else:
			fib.append(1)
			fib.append(1)
			while i <= n:
				fib.append(fib[-1] + fib[-2])
				i = i + 1
		return fib


class Solution:

	def printFibb(self, n):
		fib = []
		n1 = 0
		n2 = 1
		fib.append(n2)
		for i in range(0, n - 1):
			n3 = n1 + n2
			n1 = n2
			n2 = n3
			fib.append(n3)
		return fib


class Solution:

	def printFibb(self, n):
		t = []
		a = 1
		b = 1
		if n == 1:
			t.append(a)
		elif n == 2:
			t.append(a)
			t.append(b)
		else:
			t.append(a)
			t.append(b)
			for i in range(2, n):
				c = a + b
				t.append(c)
				a = b
				b = c
		return t


class Solution:

	def printFibb(self, n):
		n1 = 1
		n2 = 1
		listt = []
		while n > 0:
			listt.append(n1)
			nth = n1 + n2
			n1 = n2
			n2 = nth
			n = n - 1
		return listt


class Solution:

	def printFibb(self, n):
		p = 0
		arr = [0, 1]
		if n == 0:
			return 0
		if n == 1:
			return [1]
		for i in range(n - 1):
			p = arr[-2] + arr[-1]
			arr.append(p)
		return arr[1:]


class Solution:

	def __init__(self):
		self.res = []

	def printFibb(self, n, cur=1, prev=1):
		if n == 0:
			return self.res
		self.res.append(prev)
		self.printFibb(n - 1, cur + prev, cur)
		return self.res


class Solution:

	def printFibb(self, n):
		arr = []
		for i in range(n):
			if i == 0 or i == 1:
				t = 1
			else:
				sl = arr[-1]
				l = arr[-2]
				t = sl + l
			arr.append(t)
		return arr


class Solution:

	def printFibb(self, n):
		l = [1]
		i1 = 1
		i2 = 1
		if n > 1:
			l = [1, 1]
		for i in range(3, n + 1):
			s = i1 + i2
			i1 = i2
			i2 = s
			l.append(s)
		return l


class Solution:

	def printFibb(self, n):
		if n == 1:
			return '1'
		else:
			l = []
			f = 1
			j = 1
			l.append(f)
			l.append(j)
			for i in range(2, n):
				k = f + j
				l.append(k)
				f = j
				j = k
			return l


class Solution:

	def printFibb(self, n):
		if n >= 2:
			l = [1, 1]
		if n == 1:
			l = [1]
			return l
		if n == 0:
			l = []
			return l
		for i in range(2, n):
			a = l[i - 1] + l[i - 2]
			l.append(a)
		return l


class Solution:

	def printFibb(self, n):
		cur_sum = 0
		cur_list = []
		for i in range(n):
			if cur_sum == 0:
				cur_sum += 1
				cur_list.append(cur_sum)
			elif cur_sum == 1:
				cur_list.append(cur_sum)
				cur_sum += 1
			else:
				cur_sum = cur_list[i - 1] + cur_list[i - 2]
				cur_list.append(cur_sum)
		return cur_list


class Solution:

	def printFibb(self, n):
		lis = []
		lis.append(0)
		lis.append(1)
		for i in range(2, n + 1):
			val = lis[i - 1] + lis[i - 2]
			lis.append(val)
		return lis[1:]


class Solution:

	def traverse(this, el1, el2, n):
		n = n - 1
		if n == 0:
			return [el1 + el2]
		return [el1 + el2] + this.traverse(el2, el2 + el1, n)

	def printFibb(self, n):
		if n == 1:
			return [1]
		if n == 2:
			return [1, 1]
		return [1, 1] + self.traverse(1, 1, n - 2)


class Solution:

	def printFibb(self, n):
		if n == 1:
			return [1]
		if n == 2:
			return [1, 1]
		else:
			a = [1] * n
			for i in range(2, n + 1):
				a[i] = a[i - 1] + a[i - 2]
				if i == n - 1:
					break
			return a


class Solution:

	def printFibb(self, n):

		def fibo(n1, n2, cnt, n, result):
			if cnt > n:
				return result
			sum = n1 + n2
			n1 = n2
			n2 = sum
			result.append(n1)
			cnt += 1
			return fibo(n1, n2, cnt, n, result)
		return fibo(0, 1, 1, n, [])


class Solution:

	def printFibb(self, n):
		list_num = []
		for i in range(n):
			if i == 0 or i == 1:
				list_num.append(1)
			else:
				list_num.append(list_num[i - 1] + list_num[i - 2])
		return list_num


class Solution:

	def printFibb(self, n):
		if n < 2:
			return [1]
		fibbonaci_list = [1, 1]
		n1 = 1
		n2 = 1
		for i in range(n - 2):
			n3 = n1 + n2
			fibbonaci_list.append(n3)
			n1 = n2
			n2 = n3
		return fibbonaci_list


class Solution(object):

	def printFibb(self, n):
		result = []
		(num1, num2) = (0, 1)
		for _ in range(n):
			result.append(num2)
			next = num1 + num2
			(num1, num2) = (num2, next)
		return result


class Solution:

	def printFibb(self, n):
		i = 0
		c = 1
		j = 1
		a = []
		a.append(j)
		while c < n:
			b = i + j
			i = j
			j = b
			a.append(b)
			c += 1
		return a


class Solution:

	def printFibb(self, N):
		if N == 1:
			return [1]
		if N == 2:
			return [1, 1]
		ans = self.printFibb(N - 1)
		ans.append(ans[-1] + ans[-2])
		return ans


class Solution:

	def printFibb(self, n):
		if n == 0:
			return n
		fib = []
		secondlast = 0
		last = 1
		fib.append(last)
		cur = 0
		for i in range(2, n + 1, 1):
			cur = last + secondlast
			secondlast = last
			last = cur
			fib.append(cur)
		return fib