>>Wow, when you're bored, you're REALLY bored!
Tell me about it, I actually wrote it in Perl first, then ported it to C++. Next I'll add operator overloading to the Perl script. :-)
Code:
#!usr/bin/perl -w
use strict;
package Fibonacci;
sub make {
my $self = shift;
my $ref = {cache_max => 47, cache => []};
bless $ref, $self;
}
sub get {
my ($self, $sub) = @_;
if ($sub < 0 || $sub > $self->{cache_max}) {
print STDERR "Subscript is out of range\n";
}
return $self->{cache}->[$sub] if defined $self->{cache}->[$sub];
my $s = sqrt 5.0;
my $x = 0.5 * sqrt 5.0;
# Deep magic
$self->{cache}->[$sub] =
int((((0.5 + $x) ** $sub) - ((0.5 - $x) ** $sub)) / $s);
}
sub max {
my $self = shift;
$self->{cache_max};
}
package main;
my $fib = Fibonacci->make;
for (my $i = 0; $i < $fib->max; $i++) {
print $fib->get($i), "\n";
}