In: Computer Science
Blurred Sequence
Jojo as a treasure hunter has finally found the ancient treasure box. The treasure box has three layers of lock. Each layer of lock contains clues to unlock the lock.
The first layer of lock gives a sequence of numbers. But as we know that the treasure box is as old as the dinosaurs, the sequence is blurred as it goes to the end. Jojo can only read the first seven numbers of the sequence which is: 1 , 1 , 2 , 3 , 5 , 8 , 13, ... and the rest are blurred.
After a days of researching, Jojo found out that the sequence is a fibonacci sequence which each number is the sum of the two preceding ones. Formally, fibonacci sequence are written as Fi as the i-th fibonacci number and its property is Fi = Fi-1 + Fi-2 where i > 2 and in this case, the base cases are F1 = 1 and F2 = 1.
And it’s not even the riddle yet. The lock gives Jojo two numbers L and R and asks Jojo to get the sum of its digits from L-th fibonacci to R-th fibonacci inclusive. As a good friend of Jojo, help Jojo to solve the first layer lock riddle.
Format Input:
There are T testcases. Each testcase contains two integers L and R which indicates the numbers that the lock gives.
Format Output:
Output T line with format “Case # X: ”, where X indicates the testcase number and then followed by the answer of the riddle.
Constraints
• 1 ≤ T ≤ 100
• 1 ≤ L ≤ R ≤ 64
Sample Input (standard input):
5
1 4
3 5
6 7
9 9
3 11
Sample Output (standard output):
Case #1: 7
Case #2: 10
Case #3: 12
Case #4: 7
Case #5: 59
NOTE: USE C LANGUAGE, DONT USE FUNCTION(RESULT,RETURN),VOID,RECURSIVE, USE BASIC CODE AND CODE IT UNDER int main (){, constraint must be the same
#include <stdio.h>
int main()
{
int noTestCases;
int left,right;
int first=1,second=1;
int term;
int sum=0;
scanf("%d",&noTestCases); /* read number of testcases */
for(int i=1;i<=noTestCases;i++) /* loop for number of test cases */
{
scanf("%d%d",&left,&right); /* read left and right */
sum=0;
first = 1;
second = 1;
for(int j=1;j<=right;j++) /* loop until last required fibonacci number */
{
if(j==1 || j==2) /* first and second fibonacci number is 1*/
{
term=1;
}
else
{
term = first + second; /* from third onwards term is first + second */
first = second;
second = term;
}
if(j>=left) /* if current fibonacci digit is greater than equal to left then it will be part of sum */
{
while(term) /*find digits of fibonacci number and add it to sum*/
{
sum = sum + term%10;
term=term/10;
}
}
}
printf("Case #%d: %d\n",i,sum);
}
return 0;
}
please upvote if u like answer