use strict; use warnings; use v5.12; use constant TOP => 1000000; say "1 is prime."; say "2 is prime."; my $found = 2; # We already found 1 and 2. for( my $i = 3; $i < TOP; $i += 2 ) { my $qualifies = 1; for( my $j = $i - 2; $j > 1; $j -= 2 ) { if( $i % $j == 0 ){ $qualifies = 0; last; } } if( $qualifies ) { say "$i is prime."; $found++; } } say "Found $found primes between 1 and ", TOP, ".\n"; #### #include using namespace std; // First 501 primes are found from 1 to 3571 const int SEARCH_TO = 1000000; const int SEARCH_START = 3; /* * You can double check by comparing with the link below: * http://en.wikipedia.org/wiki/List_of_prime_numbers * to verify accuracy. */ int main() { cout << "1 is a prime number." << endl; cout << "2 is a prime number." << endl; int found = 2; int i; for ( i = SEARCH_START; i < SEARCH_TO; i+=2 ) { int qualifies = 1; for ( int j = i-2 ; j > 1; j-=2 ){ if ( i % j == 0 ) { qualifies = 0; break; } } if ( qualifies ) { cout << i << " is a prime number." <## use strict; use warnings; use v5.12; use constant TOP => 1000000; say "1 is prime."; say "2 is prime."; my $found = 2; # We already found 1 and 2. OUTER: for( my $i = 3; $i < TOP; $i += 2 ) { for( my $j = $i - 2; $j > 1; $j -= 2 ) { ( not $i % $j ) && next OUTER; } say "$i is prime."; $found++; } say "Found $found primes between 1 and ", TOP, ".\n"; #### ..... (long list of primes omitted ).... 59753 is prime. 59771 is prime. 59779 is prime. 59791 is prime. 59797 is prime. 59809 is prime. <-------I hit CTRL-C after this iteration. Out of memory! Use of uninitialized value $j in modulus (%) at primes.pl line 17. Use of uninitialized value $i in modulus (%) at primes.pl line 17. Illegal modulus zero at primes.pl line 17. Free to wrong pool 2f4d50 not 1000 during global destruction.