public
Last active

A Hailstone Sequence solver that prints out all sequences from 1 - 200.

  • Download Gist
HailStones.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace System;
using namespace std;
 
int main(array<System::String ^> ^args){
/* testNumber is used for the number that the hailstone program is solving for, number
is used inside the tests, its set equal to testNumber, iteration is a counter seeing how
many numbers are in each sequence, highestIteration saves iterations highest value, keepGoing
is used to keep the while loop going for solving the hailstone sequence, and count1, and 2 are used
for counting what number your on, count 2 saves the number your on, when you hit the highest iteration.
*/
int testNumber = 1, number, iteration = 0, highestIteration = 0;
bool keepGoing = true;
int count, count2 = 0;
// Loops the program for 200 times, could be set to anything though with a simple
//cin/cout prompt. For what you asked though, its set to "200".
for(count=1; count<=200; count++){
 
// Sets number = to test number, then prints out number and the first iteration.
number = testNumber;
cout << number << ".) " << number << " ";
 
// Sets keepGoing to true, ands one to the iteration, since the first iteration is
//the number itself.
keepGoing = true;
iteration = 1;
// While keepgoing is true, solve for number.
while(keepGoing==true){
// If the number is even, divide by two, print it out, and add one to the iteration.
if(number%2==0){
number/=2;
cout << number << " ";
iteration++;
}
/* But if the number is odd, multiply it by 3 and add one, print it out, and add
one to the iteration. */
else if(number%2==1){
number = (number*3)+1;
cout << number << " ";
iteration++;
}
/* If the number is 1, we don't need to run this part of the loop anymore.
So we set keepGoing to false, print out how many iterations it took. */
if(number == 1){
keepGoing = false;
cout << endl << endl;
cout << "number of iterations: " << iteration << endl << endl;
}
}
/* This checks if iterations for the current loop is bigger than the previous
highest value of iteration, then copy the Iteration number into highestIteration.*/
if (iteration > highestIteration){
highestIteration = iteration;
/* Now set the value of count2 to what number loop we're on, showing what number
uses the most amount of iterations. So the highest amt. of iterations is 125, number
175 uses that amount of iterations. */
count2 = count;
}
// Add one to value "testNumber"
testNumber++;
}
 
// Prints out the highest iteration and number that it belongs to.
cout << endl << "Highest iterations: " << highestIteration << endl;
cout << "Number: " << count2 << endl << endl;
 
cout << "Press any key to exit";
// Pauses the code.
_getch();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.