Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Hamming Sequences and Lazy Lists

by BrowserUk (Pope)
on Mar 17, 2005 at 08:34 UTC ( #440299=note: print w/replies, xml ) Need Help??

in reply to Hamming Sequences and Lazy Lists

This horribly inefficient brute-force conversion seems like it's getting close, but needs work.

#! perl -slw use strict; sub merge { return unless @_ == 2; return @{ $_[1] } unless @{ $_[0] }; return @{ $_[0] } unless @{ $_[1] }; my( $x, $y ) = ( $_[0][0], $_[1][0] ); return shift @{ $_[0] }, merge( @_ ) if $x < $y; return shift @{ $_[1] }, merge( @_ ) if $x > $y; shift @{ $_[0] }; return shift @{ $_[1] }, merge( @_ ); } my @out; sub genHam { return unless @_; my( $x, @xs ) = @_; return @out = merge [ 1, map{ $_*$x } @out ], [ genHam( @xs ) ]; } print join'|',genHam 2, 3, 5 for 1..5; __END__ [ 8:26:05.53] P:\test>440284 1 1|2|3|5 1|2|3|4|5|6|9|10|15|25 1|2|3|4|5|6|8|9|10|12|15|18|20|25|27|30|45|50|75|125 1|2|3|4|5|6|8|9|10|12|15|16|18|20|24|25|27|30|36|40|45|50|54|60|75|81| +90|100|125|135|150|225|250|375|625

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco.
Rule 1 has a caveat! -- Who broke the cabal?

Replies are listed 'Best First'.
Re^2: Hamming Sequences and Lazy Lists
by Limbic~Region (Chancellor) on Mar 17, 2005 at 16:52 UTC
    I don't follow the results? It is my understanding that the results should be the multiples of all 3 factors merged with duplicates removed. Since the list is infinite, the list should be lazy only calculating what is needed. See Re: Hamming Sequences and Lazy Lists for my understanding an implementation of the problem.

    Cheers - L~R

    Update: I still don't understand your output, but my understanding of the Hamming Sequence is wrong. I was so focused on lazy evaluation that I misunderstood "how do you generate the series of numbers composed of a given list of prime factors, where each can be used an unlimited number of times?" To me, that meant any positive multiple of any factor was valid.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://440299]
[Discipulus]: mmh.. i was no strictly thinking to programs; more on some new tutorials
[Discipulus]: but even about programs pm can be a preliminare boiler plate

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2017-09-22 21:17 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (269 votes). Check out past polls.