Advertisement
ara1123

primepairs

Oct 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define TRUE   1
  6. #define FALSE  0
  7.  
  8. int isprime(int n);
  9. int exactprime(int n);
  10. int number_of_primes(int n);
  11.  
  12. int main()
  13. {
  14.     int n1, n2, n3, c1;
  15.     n1 = 0;
  16.     n2 = 0;
  17.     n3 = 0;
  18.    
  19.     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: ");
  20.     scanf("%d", &n1);
  21.    
  22.     while (n1 != 0)
  23.     {
  24.         n3 = 0;
  25.        
  26.         if (n1 == 1)
  27.         {
  28.             while (n3 != 1)
  29.             {
  30.                 printf("\nEnter a positive integer > 5 and < 10000: ");
  31.                 scanf("%d", &n2);
  32.            
  33.                 if(n2 < 5)
  34.                 {    
  35.                     printf("Wrong input");
  36.                 }
  37.                
  38.                 else if (n2 > 10000)
  39.                 {    
  40.                     printf("Wrong input");
  41.                 }
  42.                
  43.                 else (n3++);
  44.                
  45.             }
  46.            
  47.             puts(" ");
  48.             printf("\nThe prime pairs up to %d are: \n\n", n2);
  49.            
  50.             for(c1 = 3; c1 <= n2; c1++)
  51.             {
  52.                 if (isprime(c1) && isprime(c1 + 2))
  53.                     printf("%d  , %d \n", c1, c1 + 2);
  54.             }
  55.            
  56.  
  57.         }
  58.        
  59.         else if (n1 == 2)
  60.         {
  61.             while (n3 != 1)
  62.             {
  63.                 printf("\nEnter a positive integer > 5 and < 2,000,000: ");
  64.                 scanf("%d", &n2);
  65.                
  66.                 if (n2 < 5)
  67.                     printf("Wrong input.");
  68.                
  69.                 else if (n2 > 2000000)
  70.                     printf("Wrong input.");
  71.                
  72.                 else (n3++);
  73.                
  74.             }
  75.            
  76.             puts(" ");
  77.             if (isprime(n2) == TRUE)
  78.                 printf("\n%d is a prime number \n\n", n2);
  79.            
  80.             else
  81.                 printf("\n%d is not a prime number \n\n", n2);
  82.         }
  83.        
  84.         else if (n1 == 3)
  85.        {
  86.            while (n3 != 1)
  87.            {
  88.                 printf("\nEnter a positive integer > 5 and < 2,000,000: ");
  89.                 scanf("%d", &n2);
  90.                
  91.                 if (n2 < 5)
  92.                     printf("Wrong input.");
  93.                
  94.                 else if (n2 > 2000000)
  95.                     printf("Wrong input.");
  96.                
  97.                 else (n3++);
  98.            }
  99.            
  100.             puts(" ");
  101.             printf("Exact number of primes up to %d is: %d \n", n2, exactprime(n2));
  102.             puts(" ");
  103.             printf("Approximate number of primes up to %d is: %d \n\n", n2, number_of_primes(n2));
  104.            
  105.        }
  106.        
  107.         else
  108.         {    printf("Wrong input. \n");
  109.         }    
  110.        
  111.     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: ");
  112.     scanf("%d", &n1);
  113.        
  114.     }
  115.    
  116.     printf("\n\n ***Program Terminated*** \n\n");
  117.  
  118.     return 0;
  119. }
  120.  
  121.  
  122.  
  123. /*-----------------------function isprime---------------
  124. Purpose: To determine if a given number (user input) is a prime number or not
  125. Returns: Either TRUE or FALSE to the main function
  126. ------------------------------------------------------*/
  127.  
  128. int isprime(int n)
  129. {
  130.     int c1;
  131.    
  132.     for(c1 = 2; c1 < n; c1++)
  133.     {
  134.         if(n%c1 == 0)
  135.             return FALSE;
  136.     }
  137.    
  138.     return TRUE;
  139. }
  140.  
  141.  /*-----------------------function number_of_primes--------------
  142. 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.
  143. Returns: An integer value to the main function, the integer value being approximately how many primes exist up till the user input
  144. ---------------------------------------------------------------*/
  145. int number_of_primes(int n)
  146. {
  147.     int np;
  148.     np = 0;
  149.    
  150.     np = n / (log(n) - 1);
  151.    
  152.     return np;
  153. }
  154.  
  155. int exactprime(int n)
  156. {
  157.     int c1, c2;
  158.     c2 = 1;
  159.    
  160.     for(c1 = 3; c1 <= n; c1++)
  161.             {
  162.                 if (isprime(c1))
  163.                     c2++;
  164.             }    
  165.     return c2;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement