Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Prime Number Finder

by Anonymous Monk
on Nov 01, 2013 at 14:25 UTC ( [id://1060766]=note: print w/replies, xml ) Need Help??


in reply to Prime Number Finder

Straight from 'perlthrtut' of 5.18. Just throw two numbers at it: perl primes.pl 2 100
#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Queue; sub check_num { my ($upstream, $cur_prime) = @_; my $kid; my $downstream = Thread::Queue->new(); while (my $num = $upstream->dequeue()) { next unless ($num % $cur_prime); if ($kid) { $downstream->enqueue($num); } else { print("Found prime: $num\n"); $kid = threads->create(\&check_num, $downstream, $num); } } if ($kid) { $downstream->enqueue(undef); $kid->join(); } } my $stream = Thread::Queue->new(shift()..shift(), undef); check_num($stream, 2);

Replies are listed 'Best First'.
Re^2: Prime Number Finder
by Anonymous Monk on May 27, 2014 at 21:25 UTC
    TMTOWTDI :-)
    #!/usr/bin/perl s;; ;x; while() { print if (1 x++ $\) !~ m } { $|^(..+)\1+$|^$\$} }

      Can you please explain you code? I'm a noob, honestly i didn't understand your code but it works fine.

        Managed utilize it, but I kinda butchered it. Yeah you can still use Math::Prime::Util.

        But I do not know how exactly it work. It does some magic with the output line separator.

        That if structure is hideous.

        sub primes($end) { open my $FH, '>', \my $output or die "Cannot open filehandle: $!"; $output = ""; $\ = 1; while($\ < $end) { $_ = ""; print $FH $_."\n" if( (1 x++ $\) !~ m/ {$ | ^(..+)\ +1+$ | ^\\\$\\$ }/x ); } close($FH); my @primeNumbers = split("\n", $output); shift @primeNumbers; # a blank element will get through return \@primeNumbers; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1060766]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found