Algorithm


Problem Name: beecrowd | 1176

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1176

Fibonacci Array

 

Adapted by Neilor Tonin, URI Brazil

Timelimit: 1

Write a program that reads a number and print the Fibonacci number corresponding to this read number. Remember that the first elements of the Fibonacci series are 0 and 1 and each next term is the sum of the two preceding it. All the Fibonacci numbers calculated in this program must fit in a unsigned 64 bits number.

 

Input

 

The first line of the input contains a single integer T, indicating the number of test cases. Each test case contains a single integer N (0 ≤ N ≤ 60), corresponding to the N-th term of the Fibonacci series.

 

Output

 

For each test case in the input, print the message "Fib(N) = X", where X is the N-th term of the Fibonacci series.

 

 

 

Input Sample Output Sample

3
0
4
2

Fib(0) = 0
Fib(4) = 3
Fib(2) = 1

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        if(n == 0)cout << "Fib(0) = 0\n";
        else if(n == 1)cout<<"Fib(1) = 1\n";
        else{
        ll pred = 0;
        ll before = 1;
        ll k;
        for(int i = 2; i <= n; i++)
        {
            k = pred+before;
            pred = before;
            before = k;
        }
        printf("Fib(%d) = ",n>;
        cout << k << endl;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 0 4 2

Output

x
+
cmd
Fib(0) = 0 Fib(4) = 3 Fib(2) = 1

#2 Code Example with Javascript Programming

Code - Javascript Programming


const input = require('fs').readFileSync('/dev/stdin', 'utf8');
const lines = input.split('\n');

let fibo = [0,1];
let p = 0; let s = 1;
for(var i = 0; i  <  60; i++){
    let t = s + p;
    fibo.push(t);
    p = s;
    s = t;
}
let T= parseInt(lines.shift());
for(var i = 0; i  <  T; i++){
    let N = parseInt(lines.shift());
    console.log(`Fib(${N}) = ${fibo[N]}`);
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 0 4 2

Output

x
+
cmd
Fib(0) = 0 Fib(4) = 3 Fib(2) = 1

#3 Code Example with Python Programming

Code - Python Programming


fibo=[0,1]
p=0
s=1
for i in range(60):
    t=s+p
    fibo.append(t)
    p=s
    s=t
T= int(input())
for i in range(T):
    N=int(input())
    print('Fib(%d) = %d' %(N, fibo[N]))

Copy The Code & Try With Live Editor

Input

x
+
cmd
3 0 4 2

Output

x
+
cmd
Fib(0) = 0 Fib(4) = 3 Fib(2) = 1
Advertisements

Demonstration


Previous
#1175 Beecrowd Online Judge Solution 1175 Array change I Solution in C++, Java, Js and Python
Next
#1177 Beecrowd Online Judge Solution 1177 Array Fill II Solution in C++, Java, Js and Python