Ok, try this. As pointed out by others, as written, this reads the whole number into memory to operate on it. The division sub would be trivial to change to work with file handles, only loading a bit at a time and writing out the results as it goes. The longmul sub will be a little trickier, but possible. Reference the comments by Tanktalus and ikegami. It would need to reverse the input file, operate on it, then reverse the results file.

#!/usr/bin/perl use warnings; use strict; my $number; for(0..999) { $number .= int(rand 9); } print "$number\n\n"; my ($result, $remainder) = longdiv($number, 222); print "$result\n\nRemainder $remainder\n\n\n"; print longmul($result, 222, $remainder) . "\n"; sub longdiv { my @digits = split //, shift; my $demoninator = shift; my $carry = 0; my $out = ''; my $numerator; while (@digits) { $numerator = shift @digits; $numerator += $carry * 10; $carry = $numerator % $demoninator; $out .= int($numerator / $demoninator); } $out =~ s/^0+//; return ($out, $carry); } sub longmul { my @digits = reverse split //, shift; my $multiplier = shift; my @remainder = reverse split //, shift if $_[0]; my $out = ''; my ($digit, $progress); while (@digits) { $digit = shift @digits; if ($digit eq '.') { $out = '.' . $out; next; } $progress += $digit * $multiplier; $progress += shift @remainder if @remainder; $out = ($progress % 10) . $out; $progress = int($progress / 10); } $out = $progress . $out; return $out; }

In reply to Re^3: Reversed long division by thundergnat
in thread 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.