robert44444uk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks, I want to use the exact Chinese Remainder Theorem solution in a program I am constructing but I am not confident which of the two long numbers below will be used by the scalar $crt1 in my program, or even which of the two is correct, although my gut tells me it is the solution shown by "say" is probably the one.

What coding should I use to ensure that $crt1 is always used correctly and printed correctly? Very grateful for your enlightenment.

#!/usr/bin/env perl use warnings; use strict; use Math::BigFloat lib=>"GMP"; use Math::Prime::Util qw/:all/; use feature ':5.10'; my $crt1=chinese([1,2],[1,3],[1,5],[5,7],[4,11],[9,13],[0,17],[14,19], +[22,23],[28,29],[28,31],[27,37],[3,41],[15,43],[9,47],[51,53],[55,59] +,[35,61],[17,67],[35,71],[67,73],[28,79]); say $crt1; printf ("crt solution is= %.0f\n",$crt1);

Replies are listed 'Best First'.
Re: Using big numbers correctly
by thanos1983 (Parson) on Oct 16, 2018 at 10:13 UTC

    Hello robert44444uk,

    I do not have much knowledge on but did you read this page Chinese remainder theorem/Perl?

    See sample bellow:

    #!/usr/bin/env perl use strict; use warnings; use feature qw (say); use ntheory qw/chinese lcm/; say chinese( [2328,16256], [410,5418] ), " mod ", lcm(16256,5418); # 28450328 mod 44037504 use Math::ModInt qw(mod); use Math::ModInt::ChineseRemainder qw(cr_combine); say cr_combine(mod(2,3),mod(3,5),mod(2,7)); # mod(23, 105)

    You forgot to add on your sample of code: use ntheory qw/chinese/;.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Hi BR. Thanks for the reply. The used of chinese as an instruction works in my code, the result is a long number, 954846259588805228035541587771, much greater than 2^64. My question is how to ensure that the result is used properly throughout any programming thereafter, and how the result is shown using print or printf.

      using the instruction print $crt1 -> bdstr(); produces the same result as the say instruction, which makes me think that probably $crt1 is being used correctly, but how can that instruction be used with a more complex print or printf line, whose output reads "crt solution is (put correct long number here)". Below is proof that the use of bdstr works

      #!/usr/bin/env perl use warnings; use strict; use Math::BigFloat lib=>"GMP"; use Math::Prime::Util qw/:all/; use Math::BigInt qw/:constant/; use feature ':5.10'; my $crt1=chinese([1,2],[1,3],[1,5],[5,7],[4,11],[9,13],[0,17],[14,19], +[22,23],[28,29],[28,31],[27,37],[3,41],[15,43],[9,47],[51,53],[55,59] +,[35,61],[17,67],[35,71],[67,73],[28,79]); say $crt1; print $crt1 -> bdstr();

        Well I seem to have solved the print problem with print 'crt solution =', $crt1 -> bdstr(),"\n"; So I have solved my own problem I think. 2 hours of my time I'll never get back though!