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]; }
In reply to Zeckendorf representation by thmsdrew
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |