Hello to everyone, I have a sub{} that gets a number, divies it by 222 and prints out the reasult :
#!/usr/bin/perl -w use strict; sub longdiv($) { # take the parameter passed into the sub and store in $in my $in = shift; # initialise $carry and $out variables my $carry = 0; my $out = ''; # for each position in the $in string for (my $i = 0; $i < length($in); $i++) { # take a single character at that position and store in $numer +ator my $numerator = substr($in, $i, 3); # add $carry to it $numerator += $carry; # $carry equals $numerator mod 222 $carry = ($numerator % 222); # append the integer division of $numerator by two to the end +of the $out variable $out .= int($numerator / 222); } # return the result return $out; } # call the function and print the output print longdiv2("15000000") . "\n";
now, i hope there's no problem up to here; but what i need is that function in reverse (yes... i have slight problems with mathematics...), that means that the new function should get the $carry and $out of longdive() and reasult with $in, but not by just $out*222+$carry, because i work with numbers that have thousands of digits... Thanks in advance.

In reply to Reversed long division by Smoke

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.