I've gotten a solution for Project Euler #7 in C (find the 10,001st prime). I came up with the very simple algorithm myself (from what I can tell it's similar, if not identical, to the Sieve of Eratosthenes).
#include <stdio.h>
#include <stdlib.h>
long prime_finder(int amount_of_primes);
int main(void)
{
int amount_of_primes = 0;
printf("How many primes would you like?\n");
scanf("%d", &amount_of_primes);
printf("%d: %ld\n", amount_of_primes, prime_finder(amount_of_primes));
return 0;
}
long prime_finder(int amount_of_primes)
{
int total_primes_found = 0;
long * primes = malloc(sizeof(long) * amount_of_primes);
for(int i = 0;i < amount_of_primes; i++) //set everything to 0
{
primes[i] = 0;
}
for(long i = 1; total_primes_found < amount_of_primes;i++) //find the primes
{
if(i<14) //cheat a little for the first primes
{
switch (i)
{
case 1:
break;
case 2:
primes[total_primes_found++] = 2;
break;
case 3:
primes[total_primes_found++] = 3;
break;
case 5:
primes[total_primes_found++] = 5;
break;
case 7:
primes[total_primes_found++] = 7;
break;
case 11:
primes[total_primes_found++] = 11;
break;
case 13:
primes[total_primes_found++] = 13;
break;
default:
break;
}
}
else //if it is a larger number
{
if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0
|| i % 7 == 0 || i % 11 == 0 || i % 13 == 0) /*makes the program a little quicker if the
*current number divides by a low number like 2 or 3*/
{
goto End;
}
else
{
for(int j = 0; j < total_primes_found; j++) //the brute force part of the program
{
if(i % primes[j] == 0)
{
goto End;
}
}
primes[total_primes_found++] = i;
}
End:;
}
}
for(int i = 0 ; i < amount_of_primes - 1 ; i++)
printf("%d: %ld\n", i+1, primes[i]);
return primes[amount_of_primes - 1];
}