Fibonacci numbers

This program prints out the first n numbers in the Fibonacci sequence. In case you are unfamiliar with the Fibonacci sequence, the first two numbers are both 1, and from then on each following number is equal to the sum of the previous two.

To demonstrate:

ComputationFibonacci number
11
11
1 + 12
1 + 23
2 + 35
3 + 58
5 + 813
8 + 1321
13 + 2134

Programming Issues

The computations are done using a simple for loop, using two variables (f1 and f2) to hold each pair, and a temporary variable used when updating them. The Fibonacci sequence can be computed recursively, but this is a much less efficient way in this case.

The number of numbers to calculate is passed via the command line, and extracted by ParseCmdLine(). The number must be greater than 2 (because the first two numbers are always output) and less than 48 (because unsigned long is not guaranteed to hold numbers larger than 4,294,967,295).

Usage

Pass the desired number as the sole command line argument. A sample session is shown below:

[paul@hermes fibo]$ ./fibo 47
First 47 numbers in the Fibonacci sequence:

          1          1          2          3          5
          8         13         21         34         55
         89        144        233        377        610
        987       1597       2584       4181       6765
      10946      17711      28657      46368      75025
     121393     196418     317811     514229     832040
    1346269    2178309    3524578    5702887    9227465
   14930352   24157817   39088169   63245986  102334155
  165580141  267914296  433494437  701408733 1134903170
 1836311903 2971215073

Source and Downloads