I need to write a code that takes (n) and generates n sequences of numbers (1 to n) and finds the next number starting at one according to these rules:
if a number is even number=number/2
if number is odd number=(3*number)+1
this keeps going until number<=5
For example, the sequence starting at 17 is
17 52 26 13 40 20 10 5
and its length is 7.
5 is not counted in the sequence.
The program should keep track of the length of all sequences of numbers from 1 to 17 and show which starting number will have the most numbers in its sequence. In this case the sequence that starts with 17 is the longest of sequences that start are from 1-17 inclusive, but if I enter 1000, the number with the longest sequence is 871, and that should be my output.
My code so far finds the sequence for just a single number, and gives the count.
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
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main () {
int n;
int number;
int count = 0;
cout << "Please enter a value: " << endl;
cin >> number;
while (number > 5){
if (number%2 == 0)
number=number/2;
else
number=(3*number)+1;
cout << number << ", " << endl;
count ++;
}
cout << count;
return 0;
}
|
How would I make this so that instead of my loop only taking "n" and generating a sequence for only that, takes every number between 1 and n and generates a sequence for each one?