 # PROBLEM LINK: Contest Page | CodeChef

Practice

Author: [Setter’s name]*(CodeChef User | CodeChef)

Tester: Tester’s name
Editorialist: Editorialist’s name
DIFFICULTY : BEGINNER

Nill

# PROBLEM:

Ramu, a btech final year student, is searching for a placement . He gets to know about an interview happening in his near by city.He is preparing well for his interview and is working on improving his skills in coding via an online coding platform.He is trying to solve questions by his own. But now he is stuck in writing a code of Lucas series. Help him out with the code.(Find the terms of the Lucas series up to n iterations.)

Lucas series

The Lucas series is an integer series very similar to the Fibonacci series, named after the French mathematician François Édouard Anatole Lucas. Each term of the Lucas series is defined as the sum of the previous two terms of the series with the first two terms being 2 and 1 respectively. The Lucas series and Fibonacci series are complementary to each other. The terms of the series are integer powers of the golden ratio rounded to the closest whole number.

# QUICK EXPLANATION:

Each term of the Lucas series is defined as the sum of the previous two terms of the series with the first two terms being 2 and 1 respectively.

# EXPLANATION:

We all know what a Fibonacci series is….1,1,2,3,5,8.13….and so on. For those who don’t know about it, it is a series that starts with 1 and 1 and continues to proceed in a fashion such that the next number is the sum of the previous two numbers.

Lucas series is of the same family but it starts with 2,1… instead of 1,1…The rest is just the same. 2,1,3,4,7,11…The next number is the sum of the previous two numbers.

The formula for both Fibonacci series and Lucas series is c=a+b where a and b equals to 1 in Fibonacci series and a=2 and b=1 in Lucas series.

1.Take t1=2,t2=1,tn=0 at the beginning.
2.Input n
3.check if(n==1) then
print 2.
4.else check if(n==2) then
print 2 and 1.
5.else if(n>2), print t1 and t2.
6.for(i<n-2)
7. tn=t1+t2,t1=t2,t2=t3 and print tn.
8.end for

# SOLUTIONS:

Setter's Solution

#include
using namespace std;

int main()
{
int n, i, t1 = 2, t2 = 1, tn;

``````cin >> n;

if (n == 1)
cout << endl << 2 << endl;
else if (n == 2)
cout << endl << 2 << endl << 1 << endl;
else if (n > 2)
{
cout <<endl<< t1 << endl << t2 << endl;
for (i = 0; i < n-2; i++)
{
tn = t1 + t2;
cout << tn << endl;
t1 = t2;
t2 = tn;

}
}

return 0;
``````

}

Tester's Solution

Same Person

Editorialist's Solution

Same Person