Fibonacci vs. Kilometers

This experiment was based on this Reddit posting: http://www.reddit.com/r/fifthworldproblems/comments/s0217/i_can_only_speak_in_a_recurring_meter_the/c4a4c18

Here’s a brief excerpt that sums up the point of this experiment:

Cool fact:

The Fibonacci sequence fairly accurately converts miles to kilometers. For each number in the sequence that represents miles, the next number is the same distance in kilometers

Wanting to test this, I threw together a quick Perl script to plot this out. The results were, surprisingly, “fairly accurate.”

I’ve included here a few quick plots, the numbers, and the raw source code should anyone wish to tinker further with this. Granted these are unrefined results from a presentation stand-point, but you get the point…

The raw results:

Miles, Calculated from Fibonacci, True
1,1,1.609
1,2,1.609
2,3,3.218
3,5,4.827
5,8,8.045
8,13,12.872
13,21,20.917
21,34,33.789
34,55,54.706
55,89,88.495

Miles, Percent Difference
1,-37.8495960223741
1,24.3008079552517
2,-6.77439403356122
3,3.58400662937643
5,-0.559353635798632
8,0.994406463642015
13,0.396806425395604
21,0.624463582822808
34,0.537418199100641
55,0.570653709249105
89,0.557956997507005
144,0.562806436019613
233,0.560954075386042
377,0.561661608360142
610,0.561391353961833
987,0.561494581832304
1597,0.561455152276278
2584,0.561470213023878
4181,0.561464460329799
6765,0.561466657663354
10946,0.561465818356607
17711,0.561466138943263
28657,0.561466016490064
46368,0.561466063263029
75025,0.561466045397334
121393,0.561466052221422
196418,0.561466049614862
317811,0.561466050610479
514229,0.56146605023018
832040,0.561466050375433

The source code:

 Perl | 
 
 copy code |
?

001
002
#!/usr/bin/perl
003
 
004
use GD::Graph::lines;
005
 
006
##################
007
# Computed values
008
 
009
$last = 1;
010
$cur = 1;
011
my @a;
012
 
013
print "Miles, Calculated from Fibonacci, True\n";
014
 
015
for ($i = 0; $i < 10; $i++) {
016
        $a[0][$i] = $last; #miles
017
        $a[1][$i] = $cur;  # Fibonacci Calculated value
018
        $a[2][$i] = $last * 1.609;  # computed value
019
        print $a[0][$i] . "," . $a[1][$i] . "," . $a[2][$i] . "\n";
020
 
021
        # increment the Fibonacci sequence
022
        $x = $cur; $cur += $last; $last = $x;
023
}
024
 
025
my $mygraph = GD::Graph::lines->new(600, 400);
026
$mygraph->set(
027
    x_label     => 'Miles',
028
    y_label     => 'Kilometers',
029
    title       => 'Miles to Kilometers',
030
) or warn $mygraph->error;
031
 
032
my $myimage = $mygraph->plot(\@a) or die $mygraph->error;
033
open OUTPUT, ">Fib_vs_KM.png";
034
print OUTPUT $myimage->png;
035
close(OUTPUT);
036
 
037
#################
038
# % Error 1 - 55
039
 
040
print "\nMiles, Percent Difference\n";
041
 
042
$last = 1;
043
$cur = 1;
044
my @a;
045
 
046
for ($i = 0; $i < 10; $i++) {
047
    $a[0][$i] = $last; #miles
048
    $a[1][$i] = ($cur - ($last * 1.609)) / ($last * 1.609) * 100;  # percent difference
049
 
050
        print $a[0][$i] . "," . $a[1][$i] . "\n";
051
    # increment the Fibonacci sequence
052
    $x = $cur; $cur += $last; $last = $x;
053
}
054
 
055
my $mygraph = GD::Graph::lines->new(600, 400);
056
$mygraph->set(
057
    x_label     => 'Miles',
058
    y_label     => '% Difference',
059
    title       => 'Percent Difference from Real (1 - 55 miles)',
060
) or warn $mygraph->error;
061
 
062
my $myimage = $mygraph->plot(\@a) or die $mygraph->error;
063
open OUTPUT, ">Fib_vs_KM_diff1.png";
064
print OUTPUT $myimage->png;
065
close(OUTPUT);
066
 
067
############
068
# 89 - 6765
069
 
070
for ($i = 0; $i < 10; $i++) {
071
    $a[0][$i] = $last; #miles
072
    $a[1][$i] = ($cur - ($last * 1.609)) / ($last * 1.609) * 100;  # percent difference
073
        print $a[0][$i] . "," . $a[1][$i] . "\n";
074
 
075
    # increment the Fibonacci sequence
076
    $x = $cur; $cur += $last; $last = $x;
077
}
078
 
079
my $mygraph = GD::Graph::lines->new(600, 400);
080
$mygraph->set(
081
    x_label     => 'Miles',
082
    y_label     => '% Difference',
083
    title       => 'Percent Difference from Real (89 - 6765 miles)',
084
) or warn $mygraph->error;
085
 
086
my $myimage = $mygraph->plot(\@a) or die $mygraph->error;
087
open OUTPUT, ">Fib_vs_KM_diff2.png";
088
print OUTPUT $myimage->png;
089
close(OUTPUT);
090
 
091
 
092
#################
093
# 10946 - 832040
094
 
095
for ($i = 0; $i < 10; $i++) {
096
    $a[0][$i] = $last; #miles
097
    $a[1][$i] = ($cur - ($last * 1.609)) / ($last * 1.609) * 100;  # percent difference
098
        print $a[0][$i] . "," . $a[1][$i] . "\n";
099
 
100
    # increment the Fibonacci sequence
101
    $x = $cur; $cur += $last; $last = $x;
102
}
103
 
104
my $mygraph = GD::Graph::lines->new(600, 400);
105
$mygraph->set(
106
    x_label     => 'Miles',
107
    y_label     => '% Difference',
108
    title       => 'Percent Difference from Real (10946 - 832040 miles)',
109
) or warn $mygraph->error;
110
 
111
my $myimage = $mygraph->plot(\@a) or die $mygraph->error;
112
open OUTPUT, ">Fib_vs_KM_diff3.png";
113
print OUTPUT $myimage->png;
114
close(OUTPUT);
115

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *