Hi Monks,

Today I wrote a small script for my friend to check the sum of the digits recursively (till single digit of length), numbers range from 1500 to 2300. For example:

Number 1818 1818 = 1+8+1+8 = 36 step 1 (2 digits output) 36 = 3 + 6 = 9 step 2 (single digit output) result: 9 (final count of the digits )

I wrote the below code and found the final total is printing continuously. I expected only 9 but it is printing both 9 and 18 continuously. I solved the issue later (case 2). But I want to know the reason why it is printing continuously 9 and 18. Because of that I am not getting final output '1818 == 9' in the main program.

I 'super searched' in perlmonks and found following links. subroutine recursion question,Recursion problem, Recursion. But I was not able to find correct answer. Even I googled but I didn't get correct answer. Even I flushed using $| but not able to get expected output. Where am I going wrong? Could someone explain where I am making mistake?

use strict; use warnings; my $total1; for my $num (1818..1818){ my $total1 = &spl($num); print "$num == 9\n" if ($total1 == 9); } ################# not working as i expected ####### case 1 sub spl{ my ($num1) = @_; my $total = 0; $total = $total + $_ for (split '', $num1); print "total: $total\n"; my $len = length ($total); print "length: $len\n"; &spl($total) if ($len != 1); print "final: $total\n"; return $total; } ########## working perfectly ######### case 2 #sub spl{ # #my ($num1) =@_; # #my $total = 0; # #$total = $total + $_ for (split '', $num1); # #my $len = length ($total); # #return $total if ($len == 1); # #&spl($total) if ($len != 1); # #} output: ------- total: 18 length: 2 total: 9 length: 1 final: 9 final: 18 Expected output: ---------------- total: 18 length: 2 total: 9 length: 1 final: 9 1818 == 9

Thanks in advance

Prasad

updated: added the line, 'Because of that I am not getting final output '1818 == 9' in the main program.'


In reply to Question on Recursion by prasadbabu

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.