*got to know about this bumpy sequence problem while chatting with a senior colleague last night*
The problem goes thus:
Start with any positive number n. If n is even divide it by 2, if n is odd multiply by 3 and add 1. Repeat until n becomes 1. The sequence that is generated is called a bumpy sequence. For example, the bumpy sequence that starts at n = 10 is:
10, 5, 16, 8, 4, 2, 1
and the one that starts at 11 is
11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
Write the int lengthBumpy(int n) such that:
lengthBumpy(10) returns 7, and lengthBumpy(11) returns 15.
You may assume that the argument n is positive and that the sequence converges to 1 without arithmetic overflow.
NB: Write it with any language of your choice...
No comments:
Post a Comment