Lucas Number

PROBLEM :
A Lucas Number is a number which is represented by the following recurrence
Ln = Ln-1 + Ln-2 for n>1
L0 = 2
L1 = 1

Now given a number n your task is to find the nth lucas number.

Note: Since the output could be very long take mod 1000000007

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each line contains an integer n.

Output:
For each test case in a new line print the nth lucas no.

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:
2
9
3

Output:
76
4

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int Lucas_Number(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
   cin>>no ;
   cout<<Lucas_Number(no)<<endl ;
}
return 0;
}

int Lucas_Number(int no)
{
    int arr[no+1] ;
    int i ;
   
    arr[0]=2 ;
    arr[1]=1 ;
   
    for(i=2;i<=no;i++)
        arr[i]=(arr[i-1]+arr[i-2])%1000000007 ;
       
    return arr[no] ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )