This is a program testing Ulam's Algorithm that says any positive integer would always reduce to 1 if the following algorithm was repeated a sufficient number of times:
- If the number is even, divide it by 2
- If the number is odd, multiply it by 3 and then add 1
I need to display all the numbers of each series with a maximum of 10 numbers per line. I have a couple question.
1. What kind of loop should I use for the math?
2. How do I tell the program to display only 10 terms per line?
First of all, based on your explanation of Ulam's algorithm, I don't understand why you need to ask for 2 numbers (first and last). Aren't you using the algorithm for just one number?
I also don't understand what is the "increment for testing values".
All I know is that Ulam's algorithm reduces a number to 1 in x number of iterations.
For this algorithm, do-while or while loop would be easier and more convenient.
One more thing,
You have an infinite loop at line 100.
It checks if firstNum is > 1 and if it is true then it displays that number infinitely.
Since I don't understand your goal in this program, I can't tell you how to fix it.
Sorry for not being clear.. The increment increases the first value, and the second number is how high to allow the loop to increment. Here is a sample input/output.
Program will test Ulam's algorithm
Enter the first number to test: 100
Enter the last number to test: 500
Enter the increment for testing values: 200
//ALL OF THESE SHOULD BE SETW(7) AND ONLY 10 TERMS PER LINE.
So it starts with 100, which is the first number input, increments by 200 which is the increment value input, and does that until the series increments to 500.
To output 10 values/line you can either store all values in a vector, then iterate through the vector, printing 10 items, then a new line. Or if you want to print the numbers as you are executing your program you could have a counter that increments each time you write a value, when it gets to 10 put a new line and reset it.
Hi Andy!
That's a good idea.
I knew checking for counter == 10, 20, and 30 is not the right way but I just couldn't think of a better way of doing this.