Skip to content

Instantly share code, notes, and snippets.

@jcward
Last active August 29, 2015 14:01
Show Gist options
  • Save jcward/504620eba8d63eadf976 to your computer and use it in GitHub Desktop.
Save jcward/504620eba8d63eadf976 to your computer and use it in GitHub Desktop.
Quick'n'dirty prime number calculation
#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;
}
#!/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