You made some errors, but instead of listing them all, I took your approach and tried to fix it.

You should have told us that it's Euler Problem #2

I took some liberties, especially using unshift and pop (which is kind of optional) facilitated to fix a conceptional bug of yours¹.

Feel free to ask if you have problems understanding.

HTH! :)

PS: @Harangzsolt33 I had no problems calculating for 4e12 or bigger, the limit is rather integer accuracy and not recursion overhead.

#!/usr/bin/perl use v5.14; use strict; use warnings; my $result = 0; my $level = 2; # next is F2 my $max_val = 4_000_000; my $max_rec = 1000; my $DBG = 1; fib(1,0); # F1,F0 print $result; sub fib { my @frame = @_; #my $DBG = 1; unshift @frame, $frame[0] + $frame[1]; # prepend next level Fib +(see footnote 1) pop @frame; # remove tailing Fib (opt +ional) count($frame[0]); say "Fib(" . $level++ . ") = $frame[0]" if $DBG; say " ... # \@frame = (@frame)" if $DBG; # playing safe with deep recursion die "May recursion $max_rec exceeded" if $level > $max_rec; return fib(@frame) unless($frame[0] >= $max_val); } sub count { my ($increment) = @_; #my $DBG = 1; if(($increment % 2) == 0){ say "adding Fib($level) = $increment to $result" if $DBG; $result += $increment; } }

activating the $DBG flags will show you debug information.

UPDATE

¹) you can also do a list assignment instead of unshift and pop. Your code suffered from timing problems when overwriting.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery


In reply to Re: How am i doing? by LanX
in thread How am i doing? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.