Holiday fun !!

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11163460 use warnings; use List::AllUtils qw( zip_by before ); $SIG{__WARN__} = sub { die @_ }; sub bigdivide { my ($t, $d) = map s/^0+(?=.)//r, @_; my $q = ''; my @pad = before { length $t < length $d . 0 x $_ || length $t == $_ + length $d && $t lt $d . 0 x $_ } 0 .. length $t; for( reverse @pad ) { my $digit = 0; while( length $t > $_ + length $d || length $t == $_ + length $d && $t ge $d . 0 x $_ ) { $digit++; $t = bigsubtract( $t, $d . 0 x $_ ); } $q .= $digit; } return $q || 0; } my $xx = '123888888888888888888888888888888888888888888888455555555588 +8888888888888888888888888555555555555'; my $yy = '555333577778888888888888888888888888887555555'; my $quo = bigdivide($xx, $yy); print "bigdivide $quo\n"; { use bigint; my $tmp = ($xx+0) / ($yy+0); print " bigint $tmp\n"; } sub bigsubtract { my ($top, $bottom) = map s/^0+(?=.)//r, @_; if( length $top > length $bottom || length $top == length $bottom && $top ge $bottom ) { my @top = reverse split //, $top; my @bottom = reverse split //, $bottom; my $borrow = 0; my $answer = reverse join '', zip_by { my $digit = $_[0] - ($_[1] // 0) - $borrow; $borrow = $digit >= 0 ? 0 : 1; $digit >= 0 ? $digit : $digit % 10; } \@top, \@bottom; return $answer =~ s/^0+(?=.)//r; } print "failed\n"; use Data::Dump 'dd'; dd undef; return undef; }

Outputs:

bigdivide 2230891374953314563617132232227474545270207356483795 bigint 2230891374953314563617132232227474545270207356483795

Partially tested :)


In reply to Re: Division of big integers by tybalt89
in thread Division of big integers by harangzsolt33

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.