I need to write a program that applies a certain rule to a number if it is even, and another one if it's odd. Then once the rule is applied, if the number that comes from applying the rule is 1, increment the initial value, and repeat. However, I also need to check if numbers loop and never reach one. Any suggestions for how I can do this?
I would try a while loop that continues until your number reaches one. If your number is an integer, you could do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int number = original; //Set our value to the initial value
while(/*Your condition to stop the code*/)
{
if(number % 2 == 0) //Number is even
{
//Do your action for even numbers
}
else //Not even means odd
{
//Do your action for odd numbers
}
if(number == 1)
{
number = ++original; //Increase the original and use it again
}
}
Checking whether the original value will ever reach one depends on the action you take in your loop, so I can't really tell this from your original post. I think this should give you a general idea though.
> However, I also need to check if numbers loop and never reach one.
> Any suggestions for how I can do this? http://stackoverflow.com/a/2663147
each number in your sequence would be a "node"
"moving forward" is to apply your rule