You are given integer N, and return the Fibonacci Series till the Nth term.
Example:
Input:
5
Output:
0 1 1 2 3 5
Explanation:
0 1 1 2 3 5 is the Fibonacci series up to
the 5th term.(0-based indexing)
Your Task:
You don't need to read input or print anything. Your task is to complete the function Series() which takes an Integer N as input and returns a Fibonacci Series up to the Nth term.
Expected Time Complexity: O(n)
Expected Space Complexity: O(n)
Constraint:
1<=n<100

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Complete search

Starter code: #User function Template for python3



class Solution:

    def series(self, n):

        # Code here

Solutions:
class Solution:

	def series(self, n):
		self.ans = [0, 1]

		def fib(index):
			if index > n:
				return
			self.ans.append(self.ans[index - 1] + self.ans[index - 2])
			fib(index + 1)
		fib(2)
		return self.ans


class Solution:

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


class Solution:

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


class Solution:

	def series(self, n):
		ans = [-1] * (n + 1)
		for i in range(n + 1):
			if i > 2 and ans[i - 1] > -1 and (ans[i - 2] > -1):
				ans[i] = ans[i - 1] + ans[i - 2]
			else:
				ans[i] = self.fab(i)
		return ans

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


class Solution:

	def series(self, n):
		series = [0, 1]
		while len(series) < n + 1:
			series.append(series[-1] + series[-2])
		return series


class Solution:

	def series(self, n):
		p = [0, 1]
		if n == 0:
			return [0]
		elif n == 1:
			return [0, 1]
		a = p[0]
		b = p[1]
		for i in range(3, n + 2):
			c = a + b
			p.append(c)
			a = b
			b = c
		return p


class Solution:

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


class Solution:

	def series(self, n):
		if n == 1:
			return [0, 1]
		n -= 2
		l = [0, 1]

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


class Solution:

	def __init__(self):
		self.ans = [0]

	def series(self, n):

		def solve(n, l, m):
			if n == 0:
				return
			n -= 1
			self.ans.append(m)
			solve(n, m, l + m)
		solve(n, 0, 1)
		return self.ans


class Solution:

	def series(self, n):
		s = [0, 1]
		first = 0
		second = 1
		for i in range(2, n + 1):
			res = first + second
			first = second
			second = res
			s.append(res)
		return s


class Solution:

	def series(self, n):

		def fib(idx, n, l):
			if idx > n:
				return
			sm = l[idx - 1] + l[idx - 2]
			l.append(sm)
			fib(idx + 1, n, l)
		l = [0, 1]
		fib(2, n, l)
		return l


class Solution:

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


class Solution:

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


class Solution:

	def series(self, n):
		res = [0] * (n + 1)
		res[0] = 0
		res[1] = 1
		i = 2

		def fib(res, n, i):
			if i > n:
				return
			res[i] = res[i - 1] + res[i - 2]
			fib(res, n, i + 1)
		fib(res, n, i)
		return res


class Solution:

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


class Solution:

	def fab(self, n, dp):
		if n < 2:
			return n
		if dp[n] != -1:
			return dp[n]
		dp[n] = self.fab(n - 1, dp) + self.fab(n - 2, dp)
		return dp[n]

	def series(self, n):
		dp = [-1 for i in range(101)]
		dp[0] = 0
		dp[1] = 1
		self.fab(n, dp)
		return dp[:n + 1]


class Solution:
	z = []

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


class Solution:

	def series(self, n):

		def findfibanocii(a, b, res, count):
			count += 1
			c = a + b
			res.append(c)
			if count == n:
				return res
			else:
				return findfibanocii(b, c, res, count)
		if n == 0:
			return [0]
		elif n == 1:
			return [0, 1]
		else:
			return findfibanocii(0, 1, [0, 1], 1)


class Solution:

	def series(self, n):
		arr = []
		pre2 = 0
		pre = 1
		arr.append(pre2)
		arr.append(pre)
		for i in range(2, n + 1):
			ele = pre + pre2
			arr.append(ele)
			pre2 = pre
			pre = ele
		return arr


class Solution:

	def series(self, n):
		if n == 0:
			return [0]
		if n == 1:
			return [0, 1]
		else:
			fib = self.series(n - 1)
			fib.append(fib[-1] + fib[-2])
			return fib


class Solution:

	def series(self, n):
		first = 0
		second = 1
		l = [first, second]
		for i in range(1, n):
			sum = first + second
			first = second
			second = sum
			l.append(sum)
		return l


def fib(k, dp):
	if dp[k] != -1:
		return dp[k]
	if k <= 1:
		return k
	dp[k] = fib(k - 1, dp) + fib(k - 2, dp)
	return dp[k]

class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def series(self, n):
		k = [0, 1]
		(f1, f2) = (0, 1)
		for i in range(2, n + 1):
			f3 = f1 + f2
			k.append(f3)
			f1 = f2
			f2 = f3
		return k


class Solution:

	def series(self, n):
		if n == 0:
			print(0)
			return
		if n == 1:
			print(0, 1)
			return

		def fib(k, a, b):
			if k == n - 2:
				print(a + b)
				return
			print(a + b, end=' ')
			fib(k + 1, b, a + b)
		print(0, 1, end=' ')
		fib(0, 0, 1)


import sys
sys.setrecursionlimit(10000)

class Solution:

	def series(self, n):
		if n == 1:
			return [0, 1]
		return [0, 1] + self.fibs(0, 1, 1, n)

	def fibs(self, n, m, c, t):
		if c + 1 == t:
			return [n + m]
		ans = [n + m]
		return ans + self.fibs(m, n + m, c + 1, t)


class Solution:

	def series(self, n):
		res = [0, 1]
		i = 2
		while i <= n:
			a = res[i - 1]
			b = res[i - 2]
			res.append(a + b)
			i += 1
		return res


class Solution:

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


class Solution:

	def series(self, n):
		a = 0
		b = 1
		c = 0
		x = []
		while c <= n:
			x.append(a)
			d = a + b
			a = b
			b = d
			c += 1
		return x


class Solution:

	def series(self, n):
		x = 0
		y = 1
		z = 0
		l = []
		for i in range(0, n + 1):
			l.append(z)
			x = y
			y = z
			z = x + y
		return l


class Solution:

	def series(self, n):
		ans = [0, 1]
		self.helper(n, ans)
		return ans

	def helper(self, n, ans):
		if n == 0 or n == 1:
			return
		self.helper(n - 1, ans)
		ans.append(ans[n - 1] + ans[n - 2])


class Solution:

	def series(self, n):
		if n == 0:
			return [0]
		elif n == 1:
			return [0, 1]
		elif n == 2:
			return [0, 1, 1]
		else:
			prev_series = self.series(n - 1)
			return prev_series + [prev_series[-1] + prev_series[-2]]


class Solution:

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


class Solution:

	def series(self, n):
		r = []
		a = 0
		b = 1
		c = 0
		while n + 1:
			a = b
			b = c
			c = a + b
			r.append(b)
			n -= 1
		return r


class Solution:

	def series(self, n):
		arr = [0, 1, 1]
		if n <= 2:
			return arr[:n + 1]
		for num in range(3, n + 1):
			arr.append(arr[num - 1] + arr[num - 2])
		return arr


class Solution:

	def series(self, n):
		(a, b) = (-1, 1)
		result = []
		for x in range(n + 1):
			(a, b) = (b, a + b)
			result.append(b)
		return result


class Solution:

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


class Solution:

	def series(self, n):
		res = []
		vec = [0] * (n + 1)

		def ans(i):
			if i == 1:
				return 1
			elif i == 0:
				return 0
			if vec[i] != 0:
				return vec[i]
			return ans(i - 1) + ans(i - 2)
		for j in range(n + 1):
			vec[j] = ans(j)
			res.append(vec[j])
		return res


class Solution:

	def series(self, n):
		a = 0
		b = 1
		re = [a]
		re += [b]
		for i in range(1, n):
			c = a + b
			re += [c]
			a = b
			b = c
		return re


class Solution:

	def series(self, n):
		res = [0]

		def ans(i, a, b):
			if i > n:
				return
			res.append(b)
			(a, b) = (b, a + b)
			ans(i + 1, a, b)
		ans(1, 0, 1)
		return res


class Solution:

	def series(self, n):
		ds = [0 for i in range(n + 1)]

		def helper(ds, i):
			if i > n:
				return
			ds[i] = ds[i - 1] + ds[i - 2]
			helper(ds, i + 1)
		ds[1] = 1
		helper(ds, 2)
		return ds


class Solution:

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


class Solution:

	def series(self, n):
		list = []
		for i in range(n + 1):
			list.append(self.calculate(i))
		return list

	def calculate(self, n):
		if n in [0, 1]:
			return n
		else:
			(a, b) = (0, 1)
			for i in range(n - 1):
				(a, b) = (b, a + b)
			return b


class Solution:

	def slove(self, n, a):
		if n == 0 or n == 1:
			return a
		self.slove(n - 1, a)
		a.append(a[n - 1] + a[n - 2])
		return a

	def series(self, n):
		a = []
		a.append(0)
		a.append(1)
		a = self.slove(n, a)
		return a


class Solution:

	def series(self, n):
		i = 0
		K = []
		(x, y, z) = (0, 1, 0)
		while i <= n:
			K.append(z)
			x = y
			y = z
			z = x + y
			i += 1
		return K


class Solution:

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


class Solution:

	def series(self, n):
		ans = []
		first = 0
		second = 1
		third = 0
		for i in range(n + 1):
			ans.append(third)
			first = second
			second = third
			third = first + second
		return ans


class Solution:

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


class Solution:

	def series(self, n):
		arr = [0, 1]
		return self.series_util(arr, n + 1, 0)

	def series_util(self, arr, n, i):
		if len(arr) == n:
			return arr
		arr.append(arr[i] + arr[i + 1])
		return self.series_util(arr, n, i + 1)