Question

In: Computer Science

The Lucas Numbers are a sequence very similar to the Fibonacci Sequence, the only difference being...

The Lucas Numbers are a sequence very similar to the Fibonacci Sequence, the only difference being that the Lucas Numbers start with L0 = 2 and L1 = 1 as opposed to Fibonacci’s F0 = 0 and F1 = 1. Concretely, they are defined by L0 = 2, L1 = 1, and Ln := Ln−1 + Ln−2 for n > 1. Write a function in C++ that takes an integer argument N and returns the sum of the first N + 1 even Lucas Numbers.

Solutions

Expert Solution

The source code for the same is:

Source Code:

#include<iostream>

using namespace std;

int main()
{
int num1 = 2, num2 = 1;

int N;

int tmp;

int sum=2;

int ctr=1;

cout << "Enter N: ";

cin >> N;

while(ctr!=N+1)
{
tmp = num1 + num2;

if(tmp%2==0)
{
sum+=tmp;
ctr++;
}

num1 = num2;
num2 = tmp;
}

cout << endl << "Sum of first " << ctr << " Lucas Numbers is : " << sum;

return 0;
}

Output:


Description:

Here I took 2 variables num1 and num2 that are added to get the next number. They are initialized with 2 and 1 respectively. Now, N is used for user input. The first even number is 2, so I already added it to sum which will store final sum of even numbers. ctr is used to track how many numbers added and when it reaches N+1, the loop terminates. count is initialized with 1 as we already summed 2. Now, num1 and num2 are added in temp, checked and if it is even number, it is added in sum. Whether it is even or odd, it is used to generate next number. So, the logic is like num1 = 2, num2 =1 then temp = 3. So, after checking if 3 is even or not, num1 is assigned value of num2, num2 is assigned value of temp. So, we are moving ahead 1 step so that the next number can be generated. Here, it will give 1+3=4.

First 4 even Lucas Numbers are: 2, 4, 18, 76. So, their sum is 100. (see the output in N=3)

First 6 even Lucas Numbers are: 2, 4, 18, 76, 322, 1364. So, the sum is 1786. (output for N=5).

Do comment if there is any query. Thank you. :)


Related Solutions

The Lucas numbers are very similar to the Fibonacci numbers and are defined by a1=2, a2=1,...
The Lucas numbers are very similar to the Fibonacci numbers and are defined by a1=2, a2=1, and an+2=an+1+an. So the first five are 2, 1, 3, 4, 7 and it continues in that fashion. Give the next 4 Lucas numbers
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence,...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0,...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5. Your assignment...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics and applications to arrangement of flower petals, population growth of rabbits, and genetics. For each natural number n ≥ 1, the nth Fibonacci number fn is defined inductively by f1 = 1, f2 = 2, and fn+2 = fn+1 + fn (a) Compute the first 8 Fibonacci numbers f1, · · · , f8. (b) Show that for all natural numbers n, if α...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem solved with recursion. However, this can also be done more efficiently using dynamic programming. Create a program that uses dynamic programming techniques to list the first 30 Fibonacci numbers.
Google the first 50 numbers of the Fibonacci sequence (starting with 1) to answer the following...
Google the first 50 numbers of the Fibonacci sequence (starting with 1) to answer the following questions:          (a) Test to see if the leading digits conform to Benford’s law. Do this both graphically and analytically.          (b) Using the first 10 odd numbers in the sequence as sample 1 and the first 10 even numbers in the sequence as sample 2, use Wilcoxon’s Rank-Sum to test the claim that the numbers come from different populations.          (c) Repeat (b)...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: The sequence Fn of Fibonacci numbers is defined by the recurrence relation:  which says any Nth Fibonacci number is the sum of the (N-1) and (N-2)th Fibonacci numbers. Instructions Your task will be...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers and the recursive formula please refer to problem 5 of Chapter 2 at page 81 in the text-book.) The program asks the user to input an integer and outputs the corresponding Fibonacci number. For example, if the user inputs 4, the program outputs the fifth Fibonacci number, which is 3. Introduce two local variables in the recursion function (suppose we call it fib(n)), one...
Consider the Fibonacci sequence 1,1,2,3,5,8,13,21,34,55,89,…. . The first two numbers are 1 and 1. When you...
Consider the Fibonacci sequence 1,1,2,3,5,8,13,21,34,55,89,…. . The first two numbers are 1 and 1. When you add these numbers you get 2 = 1+1, which becomes the third number in the sequence. When you add the second and third numbers, you get 3 = 1+2, which becomes the fourth number in the sequence. When you add the third and fourth numbers, you get 5 = 2+3, which becomes the fifth number in the sequence; and so on to generate the...
Q Corporation and R Inc. are two companies with very similar characteristics. The only difference between...
Q Corporation and R Inc. are two companies with very similar characteristics. The only difference between the two companies is that Q Corporation is an unlevered firm, and R Inc. is a levered firm with debt of $3.5 million and cost of debt of 10%. Both companies have earnings before interest and taxes (EBIT) of $1.5 million and a marginal corporate tax rate of 35%. Q Corporation has a cost of capital of 15%.                                       a.   What is Q Corporation’s...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT