diff options
| author | Adam Russell <ac.russell@live.com> | 2019-05-19 00:49:32 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2019-05-19 00:49:32 -0400 |
| commit | 14526d111693bd2edda7120763e3e8957c7196c8 (patch) | |
| tree | 6ec341abd3952e3fc0ff339e56f0f3b6dedbb602 | |
| parent | 03503a2734d88c446e045bb466bfc7c7ff2778a0 (diff) | |
| download | perlweeklychallenge-club-14526d111693bd2edda7120763e3e8957c7196c8.tar.gz perlweeklychallenge-club-14526d111693bd2edda7120763e3e8957c7196c8.tar.bz2 perlweeklychallenge-club-14526d111693bd2edda7120763e3e8957c7196c8.zip | |
added additional code for some thread tests
| -rw-r--r-- | challenge-008/adam-russell/perl5/ch-1t.pl | 61 | ||||
| -rw-r--r-- | challenge-008/adam-russell/perl5/ch-1xt.pl | 51 |
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-008/adam-russell/perl5/ch-1t.pl b/challenge-008/adam-russell/perl5/ch-1t.pl new file mode 100644 index 0000000000..a3913366c9 --- /dev/null +++ b/challenge-008/adam-russell/perl5/ch-1t.pl @@ -0,0 +1,61 @@ +use strict; +use warnings; +## +# Write a script that computes the first five perfect numbers. +# A perfect number is an integer that is the sum of its positive +# proper divisors (all divisors except itself). +## +use Thread; + +use constant THREAD_COUNT => 4; +use constant PERFECT_COUNT => 5; +use constant RANGE_SIZE => 100_000; +sub factor{ + my($n) = @_; + my @factors = (1); + foreach my $j (2..sqrt($n)){ + push @factors, $j if $n % $j == 0; + push @factors, ($n / $j) if $n % $j == 0 && $j ** 2 != $n; + } + return @factors; +} + +sub is_perfect{ + my @numbers = @_; + my @perfects = (); + foreach my $n (@numbers){ + my @factors = factor($n); + my $sum = unpack("%32I*", pack("I*", @factors)); + if($sum == $n){ + push @perfects, $n; + } + } + return \@perfects; +} + +## +# Main +## +my @threads; +my $count = 0; +my $lower = 2; +my $upper = RANGE_SIZE; +do{ + for(0..(THREAD_COUNT - 1)){ + my $t = Thread->new(\&is_perfect, ($lower..$upper)); + push @threads, $t; + $lower = $upper + 1; + $upper = $lower + RANGE_SIZE; + } + foreach my $t (@threads){ + my $perfects = $t->join(); + foreach my $p (@{$perfects}){ + print "$p "; + $count++; + } + } + @threads = (); +}while($count < PERFECT_COUNT); +print "\n"; + + diff --git a/challenge-008/adam-russell/perl5/ch-1xt.pl b/challenge-008/adam-russell/perl5/ch-1xt.pl new file mode 100644 index 0000000000..543978b56d --- /dev/null +++ b/challenge-008/adam-russell/perl5/ch-1xt.pl @@ -0,0 +1,51 @@ +use strict; +use warnings; +## +# Write a script that computes the first five perfect numbers. +# A perfect number is an integer that is the sum of its positive +# proper divisors (all divisors except itself). +## +use Thread; +use Perfect; + +use constant THREAD_COUNT => 4; +use constant PERFECT_COUNT => 5; +use constant RANGE_SIZE => 100_000; + +my $p = new Perfect::Perfect(); + +sub is_perfect{ + my @numbers = @_; + my @perfects = (); + foreach my $n (@numbers){ + if($p->isPerfect($n)){ + push @perfects, $n; + } + } + return \@perfects; +} + +## +# Main +## +my @threads; +my $count = 0; +my $lower = 2; +my $upper = RANGE_SIZE; +do{ + for(0..(THREAD_COUNT - 1)){ + my $t = Thread->new(\&is_perfect, ($lower..$upper)); + push @threads, $t; + $lower = $upper + 1; + $upper = $lower + RANGE_SIZE; + } + foreach my $t (@threads){ + my $perfects = $t->join(); + foreach my $p (@{$perfects}){ + print "$p "; + $count++; + } + } + @threads = (); +}while($count < PERFECT_COUNT); +print "\n"; |
