in reply to List::Utils can't find max in array of BigInts

Hello mgatto, and welcome to the Monastery!

From the information given, I cannot reproduce your problem:

#! perl use strict; use warnings; use bigint; use List::Util qw( max ); use Data::Dumper; my @result = prime_factors(600_851_475_143); #print Dumper \@result; print max @result; sub prime_factors { my ($number) = @_; my $divisor = 2; my @factors; while ($number > 1) { if (($number % $divisor) == 0) { push @factors, $divisor; $number /= $divisor; } elsif ($divisor == 2) { ++$divisor; } else { $divisor += 2; } } return @factors; }

Output:

12:58 >perl 609_SoPW.pl 6857 13:00 >

However, the problem you are seeing may be related to the problem I experienced recently with bignum and the range operator: see Strange interaction of bigint and foreach.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: List::Util can't find max in array of BigInts
by mgatto (Novice) on Apr 25, 2013 at 19:33 UTC

    Thanks for the welcome, Athanasius. I like what you did with the number separators (600_851_475_143).

    #print Dumper \@result;

    I'm curious why this dumps the reference to @result. Is there an advantage to this way of doing it?

    Lastly, my run at the problem resulted in this code:

    use Modern::Perl '2012'; # "All operators (including basic math operations) are overloaded. use bigint lib => 'Calc, GMP'; use Regexp::Common; use Data::Dumper; use List::Util qw/ max /; use Carp qw/ confess /; $SIG{__DIE__} = sub { confess @_; }; sub big_factors { my $n = shift; my @factors = [ ]; my $d = 2; # ensure $n is an integer if ($n !~ /$RE{num}{real}/) { die "Argument was not a real number."; } while ($n > 1) { while ( 0 == $n % $d) { push @factors, $d; $n /= $d; } $d += 1; if ($d**2 > $n) { if ($n > 1) { push @factors, $n; last; } } } return @factors; } my @result = big_factors(600851475143); say max @result;
      I'm curious why this dumps the reference to @result.

      Consider:

      22:45 >perl -MData::Dumper -wE "my @c = ( 5, 7, 11 ); say Dumper @c; s +ay Dumper \@c;" $VAR1 = 5; $VAR2 = 7; $VAR3 = 11; $VAR1 = [ 5, 7, 11 ]; 22:46 >

      I prefer the second form, because I can more easily see the related things grouped together (but YMMV).

      my run at the problem resulted in this code:

      And now the problem can be diagnosed! It’s in this line:

      my @factors = [ ];

      The square brackets create an empty, anonymous array, and a reference to this anonymous array becomes the first element in the named array @factors. Later, when List::Util::max is called on @factors, it tries to compare the elements in the array to one another, but fails because it doesn’t know how to compare an array reference (that first element) to a Math::BigInt object.

      The solution is to simply remove the anonymous array reference, which isn’t needed anyway:

      my @factors;

      and the code now runs without error!

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,