Last active
August 29, 2015 14:01
-
-
Save jcward/504620eba8d63eadf976 to your computer and use it in GitHub Desktop.
Quick'n'dirty prime number calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <math.h> | |
int main() | |
{ | |
std::vector<long> primes; | |
primes.push_back(2); | |
for(long i=3; i < 2^63; i++) | |
{ | |
bool prime=true; | |
for(long j=0;j<primes.size() && primes[j]*primes[j] <= i;j++) | |
{ | |
if(i % primes[j] == 0) | |
{ | |
prime=false; | |
break; | |
} | |
} | |
if(prime) | |
{ | |
primes.push_back(i); | |
std::cout << i << " "; | |
} | |
} | |
// std::cout << primes.size() << " "; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
primes = [2] | |
3.upto(1000000) { |i| | |
prime = true | |
0.upto(primes.length) { |j| | |
if (i%primes[j]==0) then | |
prime = false | |
break | |
end | |
break if (primes[j]*primes[j]>i) | |
} | |
if prime | |
primes.push(i) | |
print "#{i} " | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment