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, 68, 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 ----