Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- #include <cstdlib>
- using namespace std;
- bool isPrime(int n){ //Function determining whether or not a number is prime
- int test; // 'test' value to see if they evenly divide by 'currentCheck' (and therefore not prime)
- for (test = 2; test<=sqrt(n); test++){
- if (n % test == 0){
- return false;
- }//endif
- }//endfor
- return true;
- }
- int getNextPrime(int minimum){ // Function which cycles for testing various numbers
- do {
- minimum++;
- }while (isPrime(minimum) == false);
- return minimum ;
- }
- void printSolution (int n, bool isRear){ //Output function
- if (isRear == true){
- cout << n << " is a candidate for the rear gear" << endl;
- }else {
- cout << n << " is a candidate for the front gear" << endl;
- }//endif
- }
- int main (void) {
- // Constants
- const int lowestGear = 8,
- numberOfRearGears = 7,
- numberOfGears = 10;
- // Variables
- int currentCheck = lowestGear,
- countRear = 0,
- countFront = 0,
- countTot = 0,
- previousPrime = 0; //used to ignore adjacent primes
- bool isRear; // True or false: are there enough rear chainings accounted for?
- // Iterate through a series of prime numbers
- do {
- currentCheck = getNextPrime(currentCheck);
- if(countRear < numberOfRearGears){
- isRear = true;
- countRear=countRear+1;
- } else {
- isRear = false;
- countFront=countFront+1;
- }//endif
- printSolution(currentCheck,isRear);
- countTot = countRear + countFront;
- } while (countTot < numberOfGears);
- cout << endl << "If we are ignoring adjecent primes, then" << endl;
- // Iterate through a series of prime numbers again,
- // but this time, remove adjecent prime numbers
- currentCheck = lowestGear;
- countRear = 0;
- countFront = 0;
- // Set previousPrime to a number that will not be true for the first time.
- previousPrime = lowestGear - 3;
- do {
- currentCheck = getNextPrime(currentCheck);
- if (currentCheck != previousPrime+2){
- if(countRear < numberOfRearGears){
- isRear = true;
- countRear=countRear+1;
- } else {
- isRear = false;
- countFront=countFront+1;
- }//endif
- printSolution(currentCheck,isRear);
- previousPrime = currentCheck;
- }//endif
- countTot = countRear + countFront;
- } while (countTot < numberOfGears);
- system("PAUSE"); return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement