Question

In: Computer Science

Blurred Sequence Jojo as a treasure hunter has finally found the ancient treasure box. The treasure...

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

Solutions

Expert Solution

#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


Related Solutions

You and your partner treasure hunter have uncovered a box of coins from the ancient civilization...
You and your partner treasure hunter have uncovered a box of coins from the ancient civilization of Hyrule. The box contains 2 big coins, 3 silver coins, 4 shiny coins, and 5 tiny coins! The two of you decide to split the contents of the box evenly. You and your partner flip a coin and decide that you will have the hard job of being the divider. Then your partner will pick the pile they value more. You took a...
An ancient wooden club is found that contains 280g of carbon and has an activity of...
An ancient wooden club is found that contains 280g of carbon and has an activity of 7.3 decays per second. Determine its age assuming that in living trees the ratio of 14C/12C atoms is about 1.3
An ancient club is found that contains 189g of carbon and has an activity of 6.66...
An ancient club is found that contains 189g of carbon and has an activity of 6.66 decays per second. Determine its age in years (do not enter units) assuming that in living trees the ratio of 14C/12C atoms is 1.30×10-12.
An ancient wooden club is found that contains74g of carbon and has an activity of 7.7...
An ancient wooden club is found that contains74g of carbon and has an activity of 7.7 decays per second. Determine its age assuming that in living trees the ratio of 14C/12Catoms is about 1.3
An ancient club is found that contains 100 g of pure carbon and has an activity...
An ancient club is found that contains 100 g of pure carbon and has an activity of 6 decays per second. Determine its age assuming that in living trees the ratio of 14C/12C atoms is about 1.10 × 10-12. Note that the half life of carbon-14 is 5700 years and the Avogadro number is 6.02 × 1023.
An ancient wooden club is found that contains 80 g of carbon and has an activity...
An ancient wooden club is found that contains 80 g of carbon and has an activity of 7.5 decays per second. Determine its age assuming that in living trees the ratio of 14C/12C atoms is about 1.3×10−12.
An ancient wooden club is found that contains 68 g of carbon and has an activity...
An ancient wooden club is found that contains 68 g of carbon and has an activity of 8.4 decays per second . Determine its age assuming that in living trees the ratio of 14C/12C atoms is about 1.3×10?12.
An ancient club is found that contains 190 g of pure carbon and has an activity...
An ancient club is found that contains 190 g of pure carbon and has an activity of 4.5 decays per second. Determine its age assuming that in living trees the ratio of (14C/12C) atoms is about 1.00 × 10-12. Note that the half life of carbon-14 is 5700 years and the Avogadro number is 6.02 × 1023. Please answer in years please
An ancient club is found that contains 150 g of pure carbon and has an activity...
An ancient club is found that contains 150 g of pure carbon and has an activity of 5.5 decays per second. Determine its age assuming that in living trees the ratio of 14C/12C atoms is about 1.00 x 10^-12 . Note that the half life of carbon-14 is 5700 years and the Avogadro number is 6.02 x 10^23 .
An ancient club is found that contains 160 g of pure carbon and has an activity...
An ancient club is found that contains 160 g of pure carbon and has an activity of 6.5 decays per second. Determine its age assuming that in living trees the ratio of ( 14 C 12 C ) atoms is about 1.40 × 10 − 12 . Note that the half life of carbon-14 is 5700 years and the Avogadro number is 6.02 × 10 23. Answer in years
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT