Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. bool isPrime(int n){ //Function determining whether or not a number is prime
  9.    
  10.     int test;    //  'test' value to see if they evenly divide by 'currentCheck' (and therefore not prime)
  11.    
  12.     for (test = 2; test<=sqrt(n); test++){
  13.    
  14.         if (n % test == 0){
  15.    
  16.             return false;
  17.    
  18.         }//endif
  19.        
  20.     }//endfor
  21.    
  22.     return true;
  23. }
  24.  
  25. int getNextPrime(int minimum){ // Function which cycles for testing various numbers
  26. do {
  27.    
  28.     minimum++;
  29.    
  30. }while (isPrime(minimum) == false);
  31.  
  32. return minimum ;
  33. }
  34.  
  35.  
  36.  
  37. void printSolution (int n, bool isRear){ //Output function
  38.  
  39.     if (isRear == true){
  40.  
  41.         cout << n << " is a candidate for the rear gear" << endl;
  42.  
  43.     }else {
  44.  
  45.         cout << n << " is a candidate for the front gear" << endl;
  46.  
  47.     }//endif
  48. }
  49.  
  50.  
  51. int main (void) {
  52.  
  53.     // Constants
  54.     const int lowestGear = 8,
  55.               numberOfRearGears = 7,
  56.               numberOfGears = 10;
  57.    
  58.     // Variables
  59.     int currentCheck = lowestGear,
  60.         countRear = 0,
  61.         countFront = 0,
  62.         countTot = 0,
  63.         previousPrime = 0; //used to ignore adjacent primes
  64.     bool isRear; //   True or false: are there enough rear chainings accounted for?
  65.        
  66.     // Iterate through a series of prime numbers
  67.   do {
  68.         currentCheck = getNextPrime(currentCheck);
  69.         if(countRear < numberOfRearGears){
  70.             isRear = true;
  71.             countRear=countRear+1;
  72.         } else {
  73.             isRear = false;
  74.             countFront=countFront+1;
  75.         }//endif
  76.         printSolution(currentCheck,isRear);
  77.         countTot = countRear + countFront;
  78.     } while (countTot < numberOfGears);
  79.  
  80.  
  81.     cout << endl << "If we are ignoring adjecent primes, then" << endl;
  82.     // Iterate through a series of prime numbers again,
  83.     // but this time, remove adjecent prime numbers
  84.    
  85.     currentCheck = lowestGear;
  86.     countRear = 0;
  87.     countFront = 0;
  88.    
  89.     // Set previousPrime to a number that will not be true for the first time.
  90.     previousPrime = lowestGear - 3;
  91.    
  92.       do {
  93.         currentCheck = getNextPrime(currentCheck);
  94.         if (currentCheck != previousPrime+2){
  95.             if(countRear < numberOfRearGears){
  96.                 isRear = true;
  97.                 countRear=countRear+1;
  98.             } else {
  99.                 isRear = false;
  100.                 countFront=countFront+1;
  101.             }//endif
  102.         printSolution(currentCheck,isRear);
  103.         previousPrime = currentCheck;
  104.         }//endif
  105.         countTot = countRear + countFront;
  106.     } while (countTot < numberOfGears);
  107.  
  108.     system("PAUSE"); return 0;
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement