Fibonacci Numbers

17May13

In the twelve months from March 2013 to March 2014, I programmed solutions to the problems posted on the Contest Coding blog run by Lewis Cornwall, solving 33 problems (out of 47) and achieving a position of 4th on the leaderboard (out of 23).

As that blog has now been discontinued, I’m posting here the solutions I programmed to those problems.

Fibonacci Numbers

The Fibonacci numbers are defined as follows: the first numbers in the Fibonacci series are 0 and 1, with each subsequent number being the sum of the previous two. Find the Fibonacci number closest to 100,000.

Solution and answer (Fibonacci Numbers.pas):

{$R+}
program Fibonacci_Numbers( output );
{
Solution and answer for problem "Fibonacci Numbers" (17th May 2013) of http://ContestCoding.WordPress.com/

The last Fibonacci number < 100,000 is: 75025.
The first Fibonacci number ≥ 100,000 is: 121393.
The Fibonacci number nearest 100,000 is: 121393.

Solution programmed in Pascal using Metrowerks CodeWarrior IDE 2.1 (Discover Programming Edition); solution took ~1s to run on a 80MHz PowerPC 601.
}
var
  a, b, c, d : longint;
begin
  a := 0;
  b := 1;
  repeat
    c := a + b;
    a := b;
    b := c;
    if c < 100000 then
      d := c
  until c >= 100000;
  writeln( 'The last Fibonacci number < 100,000 is: ', d, '.' );
  writeln( 'The first Fibonacci number ≥ 100,000 is: ', c, '.' );
  a := 100000 - d;
  b := c - 100000;
  write( 'The Fibonacci number nearest 100,000 is: ' );
  if a > b then
    write( c )
  else
    write( d );
  writeln( '.' )
end.
Advertisements


No Responses Yet to “Fibonacci Numbers”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: