Project Euler Problem 25

//

So, while waiting for my pizza rolls to finish cooking, I decided that I'd do another Project Euler Problem. Here's a pretty simple solution to Project Euler Problem 25. Pretty straight forward. Just trying to figure out which term in the Fibonacci sequence is the first to contain 1000 digits (the term, not the actual number). Using bigint to handle the large numbers, and casting to a string when we print them out. Runs in 0.684s, which is fast enough for me.

  1. #!/usr/bin/perl -w
  2.  
  3. # What is the first term in the Fibonacci sequence to contain 10000 digits
  4. use strict;
  5. use bignum;
  6.  
  7. my @fibs = (1,1);
  8. my $n = 0;
  9. my $i = 2;
  10.  
  11. while(length($n) < 1000) {
  12.     $i++;
  13.     $n = fib();
  14.     push(@fibs, $n);
  15. }
  16. printf("%s: %s\n", $i, $n);
  17.  
  18. sub fib {
  19.     my $f1 = pop(@fibs);
  20.     my $f2 = pop(@fibs);
  21.     push @fibs, $f2;
  22.     push @fibs, $f1;
  23.     return $f1 + $f2;
  24. }