Skip to content

Instantly share code, notes, and snippets.

@havidad
Created November 2, 2012 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havidad/4004930 to your computer and use it in GitHub Desktop.
Save havidad/4004930 to your computer and use it in GitHub Desktop.
A Hailstone Sequence solver that prints out all sequences from 1 - 200.
#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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment