Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Heres my go without reviewing your code first.

use strict; use warnings; use overload '""'=>'stringify'; sub stringify { $_[0]->() }; sub make_sub { my ($N) = ( my @f = sort {$a <=> $b} @_ ); return bless sub{ for(;;$N++){ $N % $_ || return $N++ for @f } }; } @ARGV=(2,3,5) unless @ARGV; @ARGV=sort @ARGV; { local $"=", "; local $\="\n"; print "Factors: @ARGV"; foreach (@ARGV) { my $f=make_sub($_); print "F($_) = @{[($f) x 25]}"; } my $o=make_sub(@ARGV); print "R(@ARGV) = @{[($o) x 25]}"; print "----"; } __END__ Factors: 2, 3, 5 F(2) = 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, + 36, 38, 40, 42, 44, 46, 48, 50 F(3) = 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51 +, 54, 57, 60, 63, 66, 69, 72, 75 F(5) = 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, +85, 90, 95, 100, 105, 110, 115, 120, 125 R(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, +24, 25, 26, 27, 28, 30, 32, 33, 34 ----

Update: It looks like this is the lazy version of the iterative solution by kvale.
Update2: BrowserUk correctly points out the sort needs to be numeric, which ive fixed.

Update3: I tweaked my original a little to support a DESTROY method, and added a second more efficient variant.
Update4: Added readmore around original code.

use strict; use warnings; use overload '""'=>'stringify'; sub stringify { $_[0]->() }; sub lcf { my @f=sort {$a<=>$b} @_; my @v=shift @f; for my $f (@f) { push @v,$f unless grep !($f % $_),@v; } @v } sub make_sub1 { my ($N) = my @f = lcf( @_ ); return bless sub{ return "Simple Factors = [ @f ] Cur = $N" if @_; for(;;$N++){ $N % $_ || return $N++ for @f } }; } sub make_sub2 { my @f = my @v = lcf( @_ ); return bless sub { return "Smart Factors = [ @f ] Cur = [ @v ]" if @_; my ($min,$ret); do { $min = 0; $v[$min] >= $v[$_] and $min=$_ for 1..$#v; $ret = $v[$min]; $v[$min] += $f[$min]; } while grep !($ret % $f[$_]), 0 .. $min-1; return $ret; }; } sub DESTROY { my $s=shift; warn "DESTROY: $s ",$s->(1),"\n"; } @ARGV=(2,3,4,5,6) unless @ARGV; @ARGV=sort @ARGV; { local $"=", "; local $\="\n"; print "Factors: @ARGV"; foreach (@ARGV) { my $f=make_sub1($_); print "F($_) = @{[($f) x 25]}"; } for my $o ( make_sub1(@ARGV),make_sub2(@ARGV)) { print "R(@ARGV) = @{[($o) x 25]}"; } print "----"; } __END__ DESTROY: main=CODE(0x1ac5130) Simple Factors = [ 2 ] Cur = 51 DESTROY: main=CODE(0x1ac5130) Simple Factors = [ 3 ] Cur = 76 DESTROY: main=CODE(0x1ac5130) Simple Factors = [ 4 ] Cur = 101 DESTROY: main=CODE(0x1ac5130) Simple Factors = [ 5 ] Cur = 126 DESTROY: main=CODE(0x1ac5130) Simple Factors = [ 6 ] Cur = 151 DESTROY: main=CODE(0x1ac21c8) Smart Factors = [ 2, 3, 5 ] Cur = [ 36, + 36, 35 ] DESTROY: main=CODE(0x1c0535c) Simple Factors = [ 2, 3, 5 ] Cur = 35 Factors: 2, 3, 4, 5, 6 F(2) = 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, + 36, 38, 40, 42, 44, 46, 48, 50 F(3) = 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51 +, 54, 57, 60, 63, 66, 69, 72, 75 F(4) = 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 6 +8, 72, 76, 80, 84, 88, 92, 96, 100 F(5) = 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, +85, 90, 95, 100, 105, 110, 115, 120, 125 F(6) = 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, +102, 108, 114, 120, 126, 132, 138, 144, 150 R(2, 3, 4, 5, 6) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21 +, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34 R(2, 3, 4, 5, 6) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21 +, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34 ----
---
demerphq


In reply to Re: Challenge: Another Infinite Lazy List by demerphq
in thread Challenge: Another Infinite Lazy List by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (3)
    As of 2014-07-14 00:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (253 votes), past polls