The following script employs Zeckendorf's_theorem and prints the Zeckendorf representation for a given number.
#!/usr/bin/perl use strict; use warnings; zeck($ARGV[0]); sub zeck { if (is_fibonnacci($_[0])) { print "$_[0]\n"; exit; } my $count = 0; my $prev = 0; my $curr = 0; while (($curr = fibonnacci($count)) < $_[0]) { $prev = $curr; $count++; } print "$prev\n"; zeck($_[0] - $prev); } sub fibonnacci { my $a = 0; my $b = 1; for (0..$_[0]) { my $c = $a; $a = $b; $b = $c + $b; } return $a; } sub is_fibonnacci { my $plus = (5 * $_[0] * $_[0]) + 4; my $mins = (5 * $_[0] * $_[0]) - 4; return is_perfect_square($plus) || is_perfect_square($mins); } sub is_perfect_square { my $sqrt = int(sqrt($_[0])); return $sqrt * $sqrt == $_[0]; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Zeckendorf representation
by jwkrahn (Abbot) on Aug 25, 2012 at 02:58 UTC | |
by thmsdrew (Scribe) on Aug 25, 2012 at 03:11 UTC | |
by lidden (Curate) on Aug 25, 2012 at 03:59 UTC | |
by thmsdrew (Scribe) on Aug 27, 2012 at 03:40 UTC | |
|
Re: Zeckendorf representation
by Athanasius (Archbishop) on Aug 25, 2012 at 04:30 UTC | |
by thmsdrew (Scribe) on Aug 27, 2012 at 03:41 UTC | |
by Athanasius (Archbishop) on Aug 27, 2012 at 12:17 UTC | |
|
Re: Zeckendorf representation
by ambrus (Abbot) on Aug 25, 2012 at 11:38 UTC | |
by thmsdrew (Scribe) on Aug 27, 2012 at 03:43 UTC | |
|
Re: Zeckendorf representation (cool!)
by tye (Sage) on Aug 24, 2012 at 22:08 UTC |