Revised code
Code:
//ThousandthPrime: Calculate the thousandthprime.
#include <stdio.h>
#include <stdbool.h>
//primality function which will determine if a number is prime.
long Primality(long n)
{
//check to see if the number is a multiple of 2(ie the number is even) and is not equal to 2
if(n % 2 == 0 && n != 2)
{
return false; //if the number is even or equal to 2, return false and do not count these numbers
}//end if
//once we determine that a number is not even or equal to 2, we can check the odds.
for(long i = 3; i * i <= n; i += 2)
{
if(n % i == 0)
{
return true;
}//end if
}//end for
return false;
}//end function primality
int main()
{
long primes = 0;// counts the number of primes for us.
long n = 1; //variable that we will run through primality function
long y = 0; //variable input by the user to tell how many primes to find.
printf("Please enter a number(n), and the program will return with the \"nth\" number of primes: ");
scanf("%ld", y);
//while loop calls the primality functions until "y" primes are found
while(primes < &y)
{
n++;
Primality(n);
if(Primality(n))
{
printf("%ld", n);
primes++;
}//end if
}//end while
printf("I found %ld primes %ld is the \"nth\" prime", primes, n);
}//end main
Warnings I get--how do I increase my compiler warning level? I am using gcc on linux if it matters
Code:
ThousandthPrime.c: In function ‘main’:ThousandthPrime.c:32:2: warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]
ThousandthPrime.c:35:15: warning: comparison between pointer and integer [enabled by default]
This is what happens when I run my code.
Code:
Please enter a number(n), and the program will return with the "nth" number of primes: 9
Segmentation fault (core dumped)