So, I've been trying to learn a bit of C, because I'm tired of python being ludicrously slow.

I went around, read bits here and there and learnt enough to recreate something I was messing with recently in python, finding prime numbers. I'm now looking for a bit of critique to see if I'm doing OK or if I'm making a complete and utter mess of this.

```
#include <stdio.h> /*Need this for "printing" things innit*/
#include <math.h> /*Need this for square rooting*/
/*This function checks a given number for primality, it takes:
- n (integer): the number being tested for primality
- nSqRtIndex (integer): The index of the first prime larger than n's square root in the list of primes
- primes (array): The array of all the primes found (has to at least contain all of the primes before n's square root for the test to work)*/
int checkPrime (int n, int nSqRtIndex, int *primes){
int index = 0;/*This variable will hold the index of the prime currently being checked for divisibility into the number being checked for primality*/
while (index <= nSqRtIndex) { /*While the index being checked for-[blah, see above comment]- is less than the index of the first prime number above the number being checked's square root...*/
if (n % primes[index] == 0) {/*If the number being checked for primality is divisible by the prime at the current index in the list of primes*/
return 0; /*The function stops and returns 0 (for, "False", as in, this number isn't a prime! I found one of its factors! It's a phoney!)*/
}
index++; /*...else, the index variable is incremented by one*/
}
return 1; /*If the function hasn't stopped and returned 0 through testing all of the primes before the square root of the number being tested of primality (fecking mouthful innit), the function returns 1 (for, "True", as in, It's a prime! (not a trap)*/
}
/*main function, this is whoever is reading this.
Whoever is reading this, this is the main function...*/
int main () {
int primesToFind = 519519; /*How many primes the program will find, max seems to be 519519. 519520 ends up in a crash*/
int updateFreq = primesToFind/10; /*This variable holds how frequently an update will be printed (to show how many primes have been found) in primes (e.g. update every 1000 primes found)*/
int primes[primesToFind]; /*This array will hold all the primes*/
primes[0] = 2; /*Adding 2 to the array to begin*/
int primesFound = 1; /*This variable will hold how nmany primes the program has found, starts at one as it's used for the index at which to add a number to the primes array, and the first number (2) is already given to the primes array*/
int n = 3; /*This variable will hold the number currently being checked for primality*/
int nSqRtIndex = 0; /*This vairable will hold the index of the first prime above the current n's square root*/
while (primesFound < primesToFind) {
/*updating nSqRtIndex to point to the first prime above its square root*/
while (sqrt(n) > primes[nSqRtIndex]){ /*...so, while the square root of n (current number) is less than the number in the list of primes at the index that should point to the prime just after the square root of the number*/
nSqRtIndex++; /*One is added to the index at which the first prime above the square root of the number supposedly is*/
}
int isPrime = checkPrime(n, nSqRtIndex, primes); /*The number is tested for primality in the checkPrime function declared above main*/
if (isPrime == 1){ /*If the number is ofund to be prime...*/
primes[primesFound] = n; /*The number's added to the array of primes at the same index as there have been primes found*/
primesFound ++;/*The number of primes found is incremented by one*/
if (primesFound%updateFreq == 0){ /*If the current number of primes found mod the update frequency is equal to 0...*/
printf("Primes found: %d\nLast prime found:%d\n\n", primesFound, primes[primesFound-1]); /*An update is printed including the last prime found, and how many primes have been found*/
}
}
n++; /*The number being checked is incremented by 1*/
}
/*When the integer value of primesToFind primes have been found...*/
printf("Test complete!\nPrimes found: %d\nLast prime found:%d\n\n", primesFound, primes[primesFound-1]); /*A closing statement is printed out*/
return 0;/*main finally returns 0 (supposedly this is to say it works or something, doesn't seem to make a difference to me but whatever)*/
}
```

If this is all fine and dandy I'll continue getting to grips with this and write the one I have using the Lucas-Lehmer sequence to find Mersenne primes.

Ta