in reply to Re: Reversed long division
in thread Reversed long division

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^3: Reversed long division
by thundergnat (Deacon) on Jun 30, 2005 at 01:50 UTC

    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; }
Re^3: Reversed long division
by ikegami (Patriarch) on Jun 29, 2005 at 21:52 UTC
    Actually, his longmul needs to read the entire number into memory just like mine. Notice the reverse in my @digits = reverse split //, shift;

      Not entirely true - it seems that Smoke is going to just take the code and tweak it - and instead of looping on while (@digits), as thundergnat did, Smoke can loop on reading a single character from File::ReadBackwards at a time instead. At least, that's my impression of what Smoke is going to do.

      I still think Smoke is asking for people to write 80% of his code for him, so that he can stamp his other 20% on it and claim it as his own, based solely on his/her comments thus far. But, if we give Smoke the benefit of the doubt for a minute, this is still plausible.

      Now, if there only were a File::WriteBackwards available to use, then building $out backwards would be fine, too :-) (Which it seems thundergnat is doing as well, so I'm not sure why that's acceptable.)

        Good points.

        As for writting, I suppose you could write it out backwards initially. Then, a separate program (or further in the same program) would copy the file in reverse. That can be done as fast as copying it without reversing it.