Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-16 23:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found