I wrote a program that is supposed to take a chinese prime number factoring algorithm and print a list of x prime numbers. The code compiles with no errors and runs. But the program does nothing. Ive played around with it and havent been able to get anything to change. I don't quite know where my logic went wrong. Any ideas?
Code:
primes.h:
#include <stdio.h>
#include <stdlib.h>
int is_prime(int n); // function definition
is_prime.c:
#include "primes.h" // Includes the header file
int is_prime(int n)
{
int k, limit; // Initialization of local variables
if (n == 2) // This returns to '1' if n is equal to 2
return 1;
if (n % 2 == 0)
return 0; // The modulus determines if the number is divisible by 2 (remainder) and returns a boolerian 1 or 0 depending on the number input
limit = n / 2; // This part of the code I dont understand
for (k = 3; k <= limit; k += 2) // The for statement assigns a value of 3 to k and checks if the number is divisible by 3
if (n % k == 0) // if the modulus of n and k (3) is equal to zero it returns a 0
return 0; // returns 0 for odd non-prime numbers
return 1; // returns 1 for prime numbers
}
main.c:
#include "primes.h" // includes the header file
int limit, n;
int main(void) // main program
{
int i = 1; // counter variable
printf("PRIMES WILL BE PRINTED.\n\n"); // printed messages
printf("How many do you want to see?\n");
scanf("%d", &limit); // assigns the upper value of primes to find
while (i <= limit) { // loops until the incrementer reaches the end number of primes
is_prime(n);
if(n == 1)
printf("%6d: %10d\n",i++,n); // prints out prime number and counter, then increments the counter
else
i++; // increment counter without printing
}
return 0;
}