Tags » Prime Sieve

Prime Sieve Implementation: O(n), C++

#include <iostream>
#include <cmath>

using namespace std;
int notprime , k, a, b, c;

int main ()
	cin >> k;
	for (int i = 2; i < sqrt(k) + 1; i += 1)
		if( notprime[i] == 0 )
			for (int j = 2*i; j < k; j += i)
				notprime[j] = 1;
	cin >> a;
	for (int i = 0; i < a; i += 1)
		cin >> b;
		if ( b == 1 ) cout << ":P";
		if ( notprime[b] == 1 ) cout << "not prime";
		else cout << "prime";
	return 0;

On the numerical evaluation of factorials III

Sieving Primes

The fastest know algorithm to compute factorials uses the prime factors of the factorial. This can be done quite easily but needs a list of primes to do so. 1,624 more words


Sieving primes

When programming to calculate prime numbers you have a lot of options. The obvious (and slow) option is to make an array called primes and then loop up to a number, each time checking if the number in the loop (say x) can be divided by any of the primes that have already been calculated and if not add it to the array of primes. 324 more words


The Theory of Prime Number Classification

Prime numbers are always interesting and fun to deal with because they pose a challenge in trying to discover their hidden patterns. There are many attempts to classify prime numbers, and I decided to have a go as well. 229 more words

Expository Mathematics

Finding primes with Python

One of the Euler problems requires finding all the primes below a certain number x.  Without thinking about this very much, it’s possible to take a very easy approach. 540 more words


More about primes...The Sieve of Eratosthenes

The Sieve of Eratosthenes is the most commonly known way to generate lists of primes. Those Greeks knew what they were doing ! It is a nice simple algorithm for beginning programmers. 263 more words