Advertisement
cwchen

[Perl 6] Prime numbers in iterators

Nov 28th, 2017
3,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.48 KB | None | 0 0
  1. sub prime {
  2.     my $n = 1;
  3.    
  4.     gather {
  5.         loop {
  6.             $n++;
  7.             my $is-prime = True;
  8.  
  9.             loop (my $i = 2; $i <= $n.sqrt; $i++) {
  10.                 if $n mod $i == 0 {
  11.                     $is-prime = False;
  12.                     last;
  13.                 }
  14.             }
  15.            
  16.             if $is-prime {
  17.                 take $n;
  18.             }
  19.         }
  20.     }
  21. }
  22.  
  23. # (2 3 5 7 11 13 17 19 23 29 31)
  24. prime[10] == 31 or die "Wrong value";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement