BigFibonacci

by Felix Thompson: Y13 Age ~18

Introduction

Felix wrote this program (now available as a web version) as a solution to Problem 25 of Project Euler. It outputs all one thousand digits of the 4782nd Fibonacci number as shown below.

Output

Output

Raspberry Pi Test

Raspberry Pi Test

We modified the code to output Fibonacci numbers selected from the first three hundred, which are shown here, and confirmed in each case that the output was accurate. The program handles each digit as a separate element of an array. As usual, Felix comments the code so thoroughly that we feel no further explanation is necessary.

The Code

program BigFibonacci;
{
    Copyright (c) 2014 Felix Thompson

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
{$mode objfpc}{$H+}

uses
  Classes, Math, SysUtils;
var
  Number2, Number1, TempStore : array [1..1000] of integer;
  Count, CurrentNum : integer;

begin
  Number2[1] := 1;                                         //One is loaded as the first
  CurrentNum := 1;                                         //Fibonacci number.

  repeat
    for Count := 1 to 1000 do
      begin                                                //The larger number is stored
        TempStore[Count] := Number2[Count];                //and then has the smaller added
        Number2[Count] := Number2[Count] + Number1[Count]; //to it. The smaller number is then
        Number1[Count] := TempStore[Count];                //replaced by the stored value.
      end;

    for Count := 1 to 1000 do
      begin                                                //Each element stores one place value.
        if Number2[Count] >= 10 then                       //If a value is larger than 10 it adds one
         begin                                             //to the next element and subtracts 10
           Number2[Count] := Number2[Count] - 10;          //from the current one.
           Number2[Count + 1] := Number2[Count + 1] + 1;
         end;
      end;

    CurrentNum := CurrentNum + 1;                          //Keeps track of the place in sequence.
  until Number2[1000] <> 0;                                //Repeats until 1000th digit is filled.

  writeln('Place in sequence: ', CurrentNum);
  writeln('Entire number, digit by digit:');
  for Count := 1 to 1000 do
    write(Number2[1001 - Count]);
  readln;
end.
Programming - a skill for life!

BallTrajectory, BigFibonacci, CollatzASM and MaxCircles by Felix Thompson