in reply to Re: bigint == horrible performance?
in thread bigint == horrible performance?
It's not a hard problem to solve:
#! perl -slw use strict; use Time::HiRes qw[ time ]; my $start = time; my @res; for ( 1 .. 1e6 ) { my $c = 0; my $n = $_; while( $n > 1 ) { ++$c; $n = $n&1 ? 3*$n+1 : $n/2; if( defined $res[ $n ] ) { $c += $res[ $n ]; last; } } $res[ $_ ] = $c; } printf "Took %.6f seconds\n", time() - $start; my( $n, $i ) = 0; $n < $res[ $_ ] and ( $n, $i ) = ( $res[ $_ ], $_ ) for 1 .. $#res; print "longest sequence is $n steps starting with $i"; __END__ c:\test>collatz.pl Took 2.000000 seconds longest sequence is 524 steps starting with 837799
And the OP gave a link to his own 3 second solution, though I'm not sure why he pastebin'd it rather than posting it here.
The intriguing thing is why the OP felt the need for bigint in the first place given that using Perl's native scalars he could calculate this(*) for numbers up to 2,251,799,813,685,248 before running into precision problems.
(*)Assuming of course he had 400+ years and the exabytes of ram required to store the hash :)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: bigint == horrible performance?
by mojotoad (Monsignor) on Nov 08, 2011 at 09:39 UTC | |
by BrowserUk (Patriarch) on Nov 08, 2011 at 15:38 UTC | |
by salva (Canon) on Nov 08, 2011 at 11:20 UTC | |
by syphilis (Archbishop) on Nov 08, 2011 at 12:03 UTC | |
by mojotoad (Monsignor) on Dec 13, 2011 at 08:15 UTC | |
by salva (Canon) on Dec 20, 2011 at 13:34 UTC | |
Re^3: bigint == horrible performance?
by moodywoody (Novice) on Nov 06, 2011 at 22:21 UTC | |
by BrowserUk (Patriarch) on Nov 07, 2011 at 15:04 UTC | |
by syphilis (Archbishop) on Nov 08, 2011 at 12:17 UTC | |
by BrowserUk (Patriarch) on Nov 08, 2011 at 15:27 UTC | |
by moodywoody (Novice) on Nov 08, 2011 at 11:01 UTC |