Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define TRUE 1
- #define FALSE 0
- int isprime(int n);
- int exactprime(int n);
- int number_of_primes(int n);
- int main()
- {
- int n1, n2, n3, c1;
- n1 = 0;
- n2 = 0;
- n3 = 0;
- printf("To generate prime pairs < 10000, enter 1 \nTo check for a prime, enter 2 \nTo determine the number of primes up to a given value, enter 3 \nTo terminate the program, enter 0: ");
- scanf("%d", &n1);
- while (n1 != 0)
- {
- n3 = 0;
- if (n1 == 1)
- {
- while (n3 != 1)
- {
- printf("\nEnter a positive integer > 5 and < 10000: ");
- scanf("%d", &n2);
- if(n2 < 5)
- {
- printf("Wrong input");
- }
- else if (n2 > 10000)
- {
- printf("Wrong input");
- }
- else (n3++);
- }
- puts(" ");
- printf("\nThe prime pairs up to %d are: \n\n", n2);
- for(c1 = 3; c1 <= n2; c1++)
- {
- if (isprime(c1) && isprime(c1 + 2))
- printf("%d , %d \n", c1, c1 + 2);
- }
- }
- else if (n1 == 2)
- {
- while (n3 != 1)
- {
- printf("\nEnter a positive integer > 5 and < 2,000,000: ");
- scanf("%d", &n2);
- if (n2 < 5)
- printf("Wrong input.");
- else if (n2 > 2000000)
- printf("Wrong input.");
- else (n3++);
- }
- puts(" ");
- if (isprime(n2) == TRUE)
- printf("\n%d is a prime number \n\n", n2);
- else
- printf("\n%d is not a prime number \n\n", n2);
- }
- else if (n1 == 3)
- {
- while (n3 != 1)
- {
- printf("\nEnter a positive integer > 5 and < 2,000,000: ");
- scanf("%d", &n2);
- if (n2 < 5)
- printf("Wrong input.");
- else if (n2 > 2000000)
- printf("Wrong input.");
- else (n3++);
- }
- puts(" ");
- printf("Exact number of primes up to %d is: %d \n", n2, exactprime(n2));
- puts(" ");
- printf("Approximate number of primes up to %d is: %d \n\n", n2, number_of_primes(n2));
- }
- else
- { printf("Wrong input. \n");
- }
- printf("\nTo generate prime pairs < 10000, enter 1 \nTo check for a prime, enter 2 \nTo determine the number of primes up to a given value, enter 3 \nTo terminate the program, enter 0: ");
- scanf("%d", &n1);
- }
- printf("\n\n ***Program Terminated*** \n\n");
- return 0;
- }
- /*-----------------------function isprime---------------
- Purpose: To determine if a given number (user input) is a prime number or not
- Returns: Either TRUE or FALSE to the main function
- ------------------------------------------------------*/
- int isprime(int n)
- {
- int c1;
- for(c1 = 2; c1 < n; c1++)
- {
- if(n%c1 == 0)
- return FALSE;
- }
- return TRUE;
- }
- /*-----------------------function number_of_primes--------------
- Purpose: To determine the approximate number of primes up to a certain number, determined by user input. Uses a formula to determine this approximate number.
- Returns: An integer value to the main function, the integer value being approximately how many primes exist up till the user input
- ---------------------------------------------------------------*/
- int number_of_primes(int n)
- {
- int np;
- np = 0;
- np = n / (log(n) - 1);
- return np;
- }
- int exactprime(int n)
- {
- int c1, c2;
- c2 = 1;
- for(c1 = 3; c1 <= n; c1++)
- {
- if (isprime(c1))
- c2++;
- }
- return c2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement