in reply to Hamming Sequences and Lazy Lists
Here is a solution that is not lazy and not quite as efficient as the Haskell version, but is simple:
Update: Improved the depth bound.use Algorithm::Loops qw(NestedLoops); use List::Util qw(reduce); my @factors = (2,3,5); # Assmue increasing sequence my $seq_len = 10; my $depth = int( $seq_len ** (1/@factors)) + 1; my @seq; my @list= NestedLoops( [ ( [ 0..$depth ] ) x @factors ], sub { push @seq, reduce {$a * $b} map {$factors[$_]**$_[$_]} 0..$#_;}, ); my @sorted = sort {$a <=> $b} @seq; print "$sorted[$_] " foreach 0..$seq_len-1;
-Mark
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Hamming Sequences and Lazy Lists
by Limbic~Region (Chancellor) on Mar 17, 2005 at 16:48 UTC | |
by kvale (Monsignor) on Mar 17, 2005 at 17:38 UTC | |
by Limbic~Region (Chancellor) on Mar 17, 2005 at 18:01 UTC |
In Section
Seekers of Perl Wisdom