#!/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 $numerator 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";