Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.40 KB | None | 0 0
  1. // Orozco, Joshua
  2. // jeo575
  3. // 16175
  4. // EE312-Assignment 3
  5. // Due: 2/25/11 Friday 11:59PM
  6. // Purpose:
  7. // 1. Gets input n from user and outputs the primes from 2 to n in rows and columns in
  8. //    column-major order.
  9. // 2. Outputs pairs consecutive primes if their difference is 4.
  10. // 3. Outputs sums of 3 consecutive primes if the sum is prime also.
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <limits.h>
  15.  
  16. //Initialize two functions
  17.  
  18. //1. Multiple max
  19.  
  20. int multiple_max(int c2)
  21. {
  22.     return c2*c2;
  23. }
  24.  
  25. //2. Number of rows(n, columns)
  26.  
  27. int rows(int n, const int columns)
  28. {    //Check how many rows(n, columns) are needed
  29.     if (n % columns != 0)
  30.     {
  31.         return (n / columns) + 1;
  32.     }
  33.     else
  34.     {
  35.         return (n / columns);
  36.     }
  37. }
  38.  
  39. /*-------------------------------------------------------*/
  40.  
  41. int main()
  42. {
  43.     for(;;)
  44.     {
  45.         //Initialize input boundaries
  46.         const int MAX_INPUT = 50000;
  47.  
  48.         //Initialize integers
  49.         int input;
  50.         int integers[MAX_INPUT];
  51.  
  52.         //Initialize counters
  53.         int c1, c2, c3;
  54.  
  55.         //Initialize difference output
  56.         int difference[MAX_INPUT];
  57.         const int diff = 4;
  58.         int p1, p2;
  59.  
  60.         //Initialize 3 consec. prime output
  61.         int final_row;
  62.         const int columns = 7;
  63.         int consec[MAX_INPUT];
  64.         int max;
  65.         int check = 0;
  66.         int a1, a2, a3;
  67.         int j;
  68.  
  69.         //Initialize list of primes
  70.         int primes[MAX_INPUT];
  71.         int n = 0;
  72.  
  73.         //Initialize repeat
  74.         char answer = 'N';
  75.  
  76. /*-------------------------------------------------------*/
  77.  
  78.         //Ask user for input
  79.         printf("Input an integer from 2 to 50000 (inclusive): ");
  80.         scanf("%d", &input);
  81.         printf("\n");
  82.  
  83.         //Check if input is from 2 to 50000
  84.         if (input >= MAX_INPUT && input <= 2)
  85.         {
  86.             do
  87.             {
  88.                 printf("Bad input, please repeat: ");
  89.                 scanf("%d", &input);
  90.                 printf("\n");
  91.             }
  92.             while (input >= MAX_INPUT && input <= 2);
  93.         }
  94.  
  95. //GOOOOOOOOOOOOOOOOOOOOD
  96.  
  97.         //Initialize array as prime = 0. Composite would = 1.
  98.         for (c1 = 2; c1 <= input; c1++)
  99.         {
  100.                 integers[c1]=0;
  101.         }
  102.  
  103.         //0 and 1 are composite
  104.         integers[0] = 1;
  105.         integers[1] = 1;
  106.  
  107.         //Sieve, from 2 to i (user input)
  108.         for (c2 = 2; multiple_max(c2) <= input; c2++)
  109.         {
  110.                 if (integers[c2] == 0) //check if multiple is prime
  111.                 {
  112.                         c1 = c2;
  113.                         for (c3 = 2*c1; c3 <= input+1; c3 = c1+c3) //Go through multiples
  114.                         {
  115.                                 integers[c3] = 1; //Mark as composite
  116.                         }
  117.                 }
  118.         }
  119.  
  120.  
  121.         //Enter primes into a new array primes[n], n being the # of primes
  122.         for (c1 = 0; c1 < input+1; c1++)
  123.         {
  124.                 if (integers[c1] == 0)
  125.                 {
  126.                         primes[n] = c1;
  127.                         n++;
  128.                 }
  129.         }
  130.  
  131.         printf("\n");
  132.  
  133.         //Output number of primes
  134.         printf("There are this many primes: %d\n", n);
  135.  
  136.         //Output number of rows
  137.         printf("There are this many rows needed: %d\n", rows(n, columns));
  138.  
  139.         //See how many columns are in the final row
  140.         final_row = (n % columns);
  141.         if (final_row == 0) //If no remainder, last row had all 8 columns
  142.         {
  143.                 final_row = columns;
  144.         }
  145.  
  146.         //Output number of primes in last row
  147.         printf("There are this many primes in the last row: %d\n\n", final_row);
  148.  
  149.         //Output of primes in column-major order
  150.         printf("Table of primes in column-major order\n");
  151.  
  152.  
  153.  
  154.  
  155.  
  156.    /*     for (input = 0; input < rows(n, columns); input++) //Loop to go through each row
  157.         {
  158.             int row_value = input;  //Value of the row starts with the row number
  159.             int row_columns = columns; //Seven columns per row
  160.             if (n%row_columns == 0)
  161.             {
  162.                 if(input == rows(n, columns))  //Unless it's the last row
  163.                 row_columns = final_row;
  164.             }
  165.             else
  166.             {
  167.                 if (input == rows(n, columns)-1) //Unless it's the last row
  168.                 row_columns = final_row;
  169.             }
  170.             for (j = 0; j<row_columns; j++) //Loop to go through the columns in that row
  171.             {
  172.                 printf("%-5d ",primes[row_value]); //Output
  173.                 row_value = row_value+rows(n, columns); //Always increment the row value by the total row number
  174.                 if(j > final_row-1) //If the number is not in earlier column, subtract one (as less is added)
  175.                 {
  176.                 row_value--;
  177.                 }
  178.             }
  179.             printf("\n\n");
  180.         }
  181. */
  182.  
  183.         //Output primes where difference between two consecutive primes is 4
  184.         for (c1 = 0; c1 < n-1; c1++)
  185.         {
  186.  
  187.             difference[c1] = primes[c1+1] - primes[c1]; //Difference
  188.             p1 = primes[c1];   //Set values in array indexex to variables
  189.             p2 = primes[c1+1];
  190.             if (difference[c1] == diff)
  191.             {
  192.                 printf("The pair of primes %d, %d has a difference of %d\n", p1, p2, diff); //Output
  193.                 c2++;
  194.             }
  195.         }
  196.  
  197.         //If no outputs for differences, say no pairs exist
  198.         if (c2 == 0)
  199.         {
  200.             printf("There are no pairs of primes with a difference of %d\n", diff);
  201.         }
  202.  
  203.         printf("\n");
  204.  
  205.  
  206.         //Output primes where 3 consec. primes sum output is also prime
  207.         for (c1 = 0; c1 < n-2; c1++)
  208.         {
  209.  
  210.             consec[c1] = primes[c1] + primes[c1+1] + primes[c1+2];
  211.             a1 = primes[c1];   //Set values in array indexex to variables
  212.             a2 = primes[c1+1];
  213.             a3 = primes[c1+2];
  214.             max = consec[c1];
  215.             check = 0; //Reset check to 0
  216.             for (c2 = 2; multiple_max(c2) <= max; c2++) //Check whether max is prime or not
  217.             {
  218.                         if (max%c2 == 0) //If no remainder, means max is composite
  219.                         {
  220.                                 check++; //Increment check, check != 0 means composite
  221.                                 break;   //If so, break for efficiency
  222.                         }
  223.             }
  224.  
  225.             if (check == 0) //If check = 0, max is prime. Output sum
  226.             {
  227.                         printf("%d + %d + %d = %d (which is a CPSP of order-3)\n", a1, a2, a3, max);
  228.                         c3++;
  229.             }
  230.         }
  231.  
  232.  
  233.         //If no outputs for differences, say no 3 consec. primes exist
  234.         if (c3 == 0)
  235.         {
  236.             printf("There are does not exist a CPSP of order-3\n");
  237.         }
  238.  
  239.         printf("\n");
  240.  
  241.         //Ask for input whether to repeat or not
  242.         printf("Would you like to repeat? (Y/N): ");
  243.         answer = getchar();
  244.         scanf("%c", &answer);
  245.         printf("\n");
  246.         if (answer == 'N' || answer == 'n') //If enter 'N' or 'n', quit
  247.         {
  248.             printf("Goodbye");
  249.             getchar();
  250.             getchar();
  251.             return 0;
  252.         }
  253.         else
  254.         {
  255.             printf("\n\n"); //If repeating, skip a few lines for organization/cleanliness
  256.         }
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement