Next 2.1 Example, Primes without threads Prev

Example, Primes without threads

A program to calculate prime numbers up to 1000:

my @primes = (2); NEW_NUMBER: for my $num (3 .. 1000) { foreach (@primes) { next NEW_NUMBER if $num % $_ == 0 } print "Found prime $num\n"; push @primes, $num; }

Let's see how it is possible to parallelize the inner foreach loop by distributing the divisibility check to different threads, one thread per one prime number.

Next Perl Threads Prev