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);
print 'zeck(', $ARGV, ') = ', join(' + ', @zeck), "\n";
return $_ if is_fibonnacci($_);
my $count = 0;
my $prev = 0;
my $curr = 0;
while (($curr = fibonnacci($count)) < $_)
$prev = $curr;
return ($prev, zeck($_ - $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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||