http://www.perlmonks.org?node_id=989689


in reply to Zeckendorf representation

Hello thmsdrew,

I agree with tye — this is cool! And I’m impressed with the speed at which the calculations are done; this calculation:

zeck(1019377012345) = 956722026041 + 53316291173 + 7778742049 + 113490 +3170 + 267914296 + 102334155 + 39088169 + 14930352 + 514229 + 196418 ++ 46368 + 17711 + 6765 + 987 + 377 + 55 + 21 + 8 + 1

(see code below) takes less than a second on my machine!

My only quibble is with the design of the zeck subroutine. A sub containing exit is a red flag for me, and even print statements are better deferred to the caller. To my mind, zeck should return the sum as a list of Fibonacci numbers. And in Perl, this is surprisingly easy to do:

my @zeck = zeck($ARGV[0]); print 'zeck(', $ARGV[0], ') = ', join(' + ', @zeck), "\n"; sub zeck { return $_[0] if is_fibonnacci($_[0]); my $count = 0; my $prev = 0; my $curr = 0; while (($curr = fibonnacci($count)) < $_[0]) { $prev = $curr; $count++; } return ($prev, zeck($_[0] - $prev)); }

Note that by restricting sub zeck to a single responsibility, the code becomes both more flexible and shorter.

Again, thanks for a cool demo!

P.S. I actually find jwkrahn’s one-line version of sub is_perfect_square to be clearer than the original. I guess clarity is in the eye of the beholder!

Athanasius <°(((><contra mundum