|
| 1 | +--- |
| 2 | +id: nth-fibonacci-number |
| 3 | +title: Nth Fibonacci Number |
| 4 | +sidebar_label: Nth Fibonacci Number |
| 5 | +tags: |
| 6 | + - Easy |
| 7 | + - Dynamic Programming |
| 8 | + - Math |
| 9 | +description: "This tutorial covers the solution to the Nth Fibonacci Number problem from the GeeksforGeeks." |
| 10 | +--- |
| 11 | +## Problem Description |
| 12 | + |
| 13 | +Given a positive integer `n`, find the nth Fibonacci number. Since the answer can be very large, return the answer modulo `1000000007`. |
| 14 | + |
| 15 | +Note: For this question, take the first Fibonacci number to be 1. |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +``` |
| 22 | +Input: n = 1 |
| 23 | +Output: 1 |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +``` |
| 29 | +Input: n = 5 |
| 30 | +Output: 5 |
| 31 | +``` |
| 32 | + |
| 33 | +## Your Task |
| 34 | + |
| 35 | +You don't need to read input or print anything. Your task is to complete the function `nthFibonacci()` which takes the integer `n` as input and returns the nth Fibonacci number modulo `1000000007`. |
| 36 | + |
| 37 | +Expected Time Complexity: $O(n)$ |
| 38 | + |
| 39 | +Expected Auxiliary Space: $O(n)$ for dynamic programming or $O(1)$ for iterative approach. |
| 40 | + |
| 41 | +## Constraints |
| 42 | + |
| 43 | +* `1 ≤ n ≤ 10^7` |
| 44 | + |
| 45 | +## Problem Explanation |
| 46 | + |
| 47 | +The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 1 and 1. The nth Fibonacci number can be computed using the formula: `F(n) = F(n-1) + F(n-2)`. For large values of `n`, the result should be returned modulo `1000000007`. |
| 48 | + |
| 49 | +## Code Implementation |
| 50 | + |
| 51 | +<Tabs> |
| 52 | + <TabItem value="Python" label="Python" default> |
| 53 | + <SolutionAuthor name="@arunimad6yuq"/> |
| 54 | + |
| 55 | + ```py |
| 56 | + MOD = 1000000007 |
| 57 | + |
| 58 | + class Solution: |
| 59 | + def nthFibonacci(self, n: int) -> int: |
| 60 | + if n == 1 or n == 2: |
| 61 | + return 1 |
| 62 | + |
| 63 | + a, b = 1, 1 |
| 64 | + for i in range(3, n + 1): |
| 65 | + a, b = b, (a + b) % MOD |
| 66 | + |
| 67 | + return b |
| 68 | + |
| 69 | + # Example usage |
| 70 | + if __name__ == "__main__": |
| 71 | + solution = Solution() |
| 72 | + print(solution.nthFibonacci(1)) # Expected output: 1 |
| 73 | + print(solution.nthFibonacci(5)) # Expected output: 5 |
| 74 | + ``` |
| 75 | + |
| 76 | + </TabItem> |
| 77 | + <TabItem value="C++" label="C++"> |
| 78 | + <SolutionAuthor name="@YourUsername"/> |
| 79 | + |
| 80 | + ```cpp |
| 81 | + //{ Driver Code Starts |
| 82 | + #include <bits/stdc++.h> |
| 83 | + using namespace std; |
| 84 | + |
| 85 | + // } Driver Code Ends |
| 86 | + class Solution { |
| 87 | + public: |
| 88 | + // Function to find nth Fibonacci number |
| 89 | + int nthFibonacci(int n) { |
| 90 | + const int MOD = 1000000007; |
| 91 | + if (n == 1 || n == 2) return 1; |
| 92 | + |
| 93 | + int a = 1, b = 1, c; |
| 94 | + for (int i = 3; i <= n; ++i) { |
| 95 | + c = (a + b) % MOD; |
| 96 | + a = b; |
| 97 | + b = c; |
| 98 | + } |
| 99 | + return b; |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + //{ Driver Code Starts. |
| 104 | + int main() { |
| 105 | + int t; |
| 106 | + cin >> t; |
| 107 | + while (t--) { |
| 108 | + int n; |
| 109 | + cin >> n; |
| 110 | + Solution obj; |
| 111 | + cout << obj.nthFibonacci(n) << endl; |
| 112 | + } |
| 113 | + return 0; |
| 114 | + } |
| 115 | + // } Driver Code Ends |
| 116 | + ``` |
| 117 | +
|
| 118 | + </TabItem> |
| 119 | +
|
| 120 | + <TabItem value="Javascript" label="Javascript" default> |
| 121 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 122 | +
|
| 123 | + ```javascript |
| 124 | +class Solution { |
| 125 | + nthFibonacci(n) { |
| 126 | + if (n === 1 || n === 2) return 1; |
| 127 | + let a = 1, b = 1; |
| 128 | + for (let i = 3; i <= n; i++) { |
| 129 | + [a, b] = [b, (a + b) % 1000000007]; |
| 130 | + } |
| 131 | + return b; |
| 132 | + } |
| 133 | +}ut: 5 |
| 134 | +
|
| 135 | + ``` |
| 136 | + |
| 137 | + </TabItem> |
| 138 | + |
| 139 | + <TabItem value="Typescript" label="Typescript" default> |
| 140 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 141 | + |
| 142 | + ```typescript |
| 143 | +class Solution { |
| 144 | + nthFibonacci(n: number): number { |
| 145 | + if (n === 1 || n === 2) return 1; |
| 146 | + let a: number = 1, b: number = 1; |
| 147 | + for (let i: number = 3; i <= n; i++) { |
| 148 | + [a, b] = [b, (a + b) % 1000000007]; |
| 149 | + } |
| 150 | + return b; |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | + ``` |
| 155 | + |
| 156 | + </TabItem> |
| 157 | + |
| 158 | + <TabItem value="Java" label="Java" default> |
| 159 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 160 | + |
| 161 | + ```java |
| 162 | +public class Solution { |
| 163 | + public int nthFibonacci(int n) { |
| 164 | + if (n == 1 || n == 2) return 1; |
| 165 | + int a = 1, b = 1; |
| 166 | + for (int i = 3; i <= n; i++) { |
| 167 | + int sum = (a + b) % 1000000007; |
| 168 | + a = b; |
| 169 | + b = sum; |
| 170 | + } |
| 171 | + return b; |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | + ``` |
| 176 | + |
| 177 | + </TabItem> |
| 178 | +</Tabs> |
| 179 | + |
| 180 | + |
| 181 | +## Time Complexity |
| 182 | + |
| 183 | +* The iterative approach has a time complexity of $O(n)$. |
| 184 | + |
| 185 | +## Space Complexity |
| 186 | + |
| 187 | +* The space complexity is $O(1)$ since we are using only a fixed amount of extra space. |
0 commit comments