# Sum of numbers in the Kth level of a Fibonacci triangle

Given a number K, the task is to find the sum of numbers at the Kth level of the Fibonacci triangle.

Examples:

```Input: K = 3
Output: 10
Explanation:
Fibonacci triangle till level 3:
0
1 1
2 3 5
Sum at 3rd level = 2 + 3 + 5 = 10

Input: K = 2
Output: 2
Explanation:
Fibonacci triangle till level 3:
0
1 1
Sum at 3rd level = 1 + 1 = 2
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Till Kth level, i.e. from level [1, K-1], count of Fibonacci numbers already used can be computed as:
```cnt = N(Level 1) + N(Level 2)
+ N(Level 3) + ...
+ N(Level K-1)
= 1 + 2 + 3 + ... + (K-1)
= K*(K-1)/2
```
2. Also, we know that the Kth level will contain K Fibonacci numbers.
3. Therfore we can find the numbers in the Kth level as Fibonacci numbers in the range [(cnt + 1), (cnt + 1 + K)].
4. We can find the sum of Fibonacci numbers in a range in O(1) time using Binet’s Formula.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find ` `// the Sum of numbers in the ` `// Kth level of a Fibonacci triangle ` ` `  `#include ` `using` `namespace` `std; ` `#define MAX 1000000 ` ` `  `// Function to return ` `// the nth Fibonacci number ` `int` `fib(``int` `n) ` `{ ` `    ``double` `phi = (1 + ``sqrt``(5)) / 2; ` `    ``return` `round(``pow``(phi, n) / ``sqrt``(5)); ` `} ` ` `  `// Function to return ` `// the required sum of the array ` `int` `calculateSum(``int` `l, ``int` `r) ` `{ ` ` `  `    ``// Using our deduced result ` `    ``int` `sum = fib(r + 2) - fib(l + 1); ` ` `  `    ``return` `sum; ` `} ` ` `  `// Function to return the sum of ` `// fibonacci in the Kth array ` `int` `sumFibonacci(``int` `k) ` `{ ` `    ``// Count of fibonacci which are in ` `    ``// the arrays from 1 to k - 1 ` `    ``int` `l = (k * (k - 1)) / 2; ` `    ``int` `r = l + k; ` ` `  `    ``int` `sum = calculateSum(l, r - 1); ` ` `  `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `k = 3; ` ` `  `    ``cout << sumFibonacci(k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to find ` `// the Sum of numbers in the ` `// Kth level of a Fibonacci triangle ` `import` `java.util.*;  ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to return ` `// the nth Fibonacci number ` `static` `int` `fib(``int` `n) ` `{ ` `    ``double` `phi = (``1` `+ Math.sqrt(``5``)) / ``2``; ` `    ``return` `(``int``)Math.round(Math.pow(phi, n) / Math.sqrt(``5``)); ` `} ` ` `  `// Function to return ` `// the required sum of the array ` `static` `int` `calculateSum(``int` `l, ``int` `r) ` `{ ` ` `  `    ``// Using our deduced result ` `    ``int` `sum = fib(r + ``2``) - fib(l + ``1``); ` ` `  `    ``return` `sum; ` `} ` ` `  `// Function to return the sum of ` `// fibonacci in the Kth array ` `static` `int` `sumFibonacci(``int` `k) ` `{ ` `    ``// Count of fibonacci which are in ` `    ``// the arrays from 1 to k - 1 ` `    ``int` `l = (k * (k - ``1``)) / ``2``; ` `    ``int` `r = l + k; ` ` `  `    ``int` `sum = calculateSum(l, r - ``1``); ` ` `  `    ``return` `sum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[])  ` `{  ` ` `  `    ``int` `k = ``3``; ` ` `  `    ``System.out.println(sumFibonacci(k)); ` `} ` `} ` ` `  `// This code is contributed by AbhiThakur `

## Python3

 `# Python3 implementation to find  ` `# the Sum of numbers in the  ` `# Kth level of a Fibonacci triangle  ` ` `  `import` `math ` `MAX` `=` `1000000`  ` `  `# Function to return  ` `# the nth Fibonacci number  ` `def` `fib(n):  ` ` `  `    ``phi ``=` `(``1` `+` `math.sqrt(``5``)) ``/` `2` `    ``return` `round``(``pow``(phi, n) ``/` `math.sqrt(``5``)) ` `  `  ` `  `# Function to return  ` `# the required sum of the array  ` `def` `calculateSum(l, r): ` ` `  `    ``# Using our deduced result  ` `    ``sum` `=` `fib(r ``+` `2``) ``-` `fib(l ``+` `1``) ` ` `  `    ``return` `sum` ` `  `# Function to return the sum of  ` `# fibonacci in the Kth array  ` `def` `sumFibonacci(k) : ` `    ``# Count of fibonacci which are in  ` `    ``# the arrays from 1 to k - 1  ` `    ``l ``=` `(k ``*` `(k ``-` `1``)) ``/` `2` `    ``r ``=` `l ``+` `k ` ` `  `    ``sum` `=` `calculateSum(l, r ``-` `1``)  ` ` `  `    ``return` `sum` ` `  `# Driver code  ` `k ``=` `3`  ` `  `print``(sumFibonacci(k)) ` ` `  `# This code is contributed by Sanjit_Prasad `

## C#

 `// C# implementation to find ` `// the Sum of numbers in the ` `// Kth level of a Fibonacci triangle ` `using` `System;  ` ` `  `class` `GFG   ` `{ ` `   `  `// Function to return ` `// the nth Fibonacci number ` `static` `int` `fib(``int` `n) ` `{ ` `    ``double` `phi = (1 + Math.Sqrt(5)) / 2; ` `    ``return` `(``int``)Math.Round(Math.Pow(phi, n) / Math.Sqrt(5)); ` `} ` `  `  `// Function to return ` `// the required sum of the array ` `static` `int` `calculateSum(``int` `l, ``int` `r) ` `{ ` `  `  `    ``// Using our deduced result ` `    ``int` `sum = fib(r + 2) - fib(l + 1); ` `  `  `    ``return` `sum; ` `} ` `  `  `// Function to return the sum of ` `// fibonacci in the Kth array ` `static` `int` `sumFibonacci(``int` `k) ` `{ ` `    ``// Count of fibonacci which are in ` `    ``// the arrays from 1 to k - 1 ` `    ``int` `l = (k * (k - 1)) / 2; ` `    ``int` `r = l + k; ` `  `  `    ``int` `sum = calculateSum(l, r - 1); ` `  `  `    ``return` `sum; ` `} ` `  `  `// Driver code ` `public` `static` `void` `Main()   ` `{   ` `  `  `    ``int` `k = 3; ` `  `  `    ``Console.Write(sumFibonacci(k)); ` `} ` `} ` ` `  `// This code is contributed by mohit kumar 29 `

Output:

```10
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.