I found this site "Project Euler" that has many programming challenges.

link.

I just started taking a CS class and found programming to be fun, so I tried my hand at some of these problems..

I have made a prime number generator for some of the easier problems, but they were rather inefficient because it divides every odd number by half of the odd numbers below it... So the more it calculates, the slower it becomes.. The problem I'm on requires me to calculate prime numbers up to 2 million.

So I decided to edit the algorithm a bit so that it divides every odd number by previous prime numbers, but it stops as soon as it calculates 3 prime numbers.

Can anyone see why my program stops running after just a bit?

Code:

#include<stdio.h>
main(void) {
int number, prime, i, j;
// max defines the upper-bound.
// Example: find all prime numbers between 1-900000
int max = 900000;
// amount defines the amount of prime numbers to calculate
// Example: Calculate 10001 prime numbers
int amount = 10001;
int primes[] = {2,3,5,7};
j = 3;
printf("2\n");
for(number = 7; number < max && j < amount; number = number + 2) {
prime = 1;
for(i = 0; i <= j; i++) {
if(number % primes[i] == 0)
prime = 0;
}
if(prime) {
printf("%d\n", number);
j++;
primes[j] = number;
}
}
}