CCO '98 P1 - Fibonacci Numbers

View as PDF

Submit solution

Points: 10
Time limit: 2.0s
Memory limit: 64M

Problem type
Allowed languages
C, C++, ObjC, Pascal
Canadian Computing Competition: 1998 Stage 2, Day 1, Problem 1

The nth Fibonacci number, f(n), is defined thus:

\displaystyle \begin{cases}
f(1) = 1 \\
f(2) = 1 \\
f(n) = f(n-1) + f(n-2) & \text{for all } n > 2
\end{cases}

Write a program that reads several n, one per line, and writes the corresponding f(n), one per line. Each value of n will be between 1 and 200. The last line of input contains 0.

Sample Input

1
2
3
4
5
100
0

Sample Output

1
1
2
3
5
354224848179261915075

Comments


  • 1
    AryanG  commented on June 8, 2019, 12:23 p.m.

    Sorry I just checked again I have a few issues I just found.


  • 1
    AryanG  commented on June 6, 2019, 9:34 a.m.

    Why does the judge keep saying my answer is wrong even though it works on repl.it


    • -1
      wleung_bvg  commented on June 6, 2019, 10:38 a.m.

      Your answers are incorrect for (somewhat) large values of n.


      • 1
        AryanG  commented on June 8, 2019, 12:18 p.m. edited

        I have double checked the numbers my program is outputting and they are exactly correct. Also, the judge fails me for a relatively low value of fib(20).


        • 1
          cup_of_tea  commented on Sept. 18, 2019, 8:24 a.m.

          I think the submission feedback is troncated, displaying only the first elements (even if they are correct).

          I had a similar error for very big results, but I had the same reaction, fib(20) being the last line displayed in feedback, while I was sure it was working for this input.

          Maybe verify very big results (like 200) using a webgenerator for fibonaccis number.


          • -1
            wleung_bvg  commented on Sept. 18, 2019, 4:05 p.m.

            The issue AryanG had was actually related to how the answers were stored. In C++, long double is definitely not precise enough to store the correct answers for large values of n.