moosemaster23
New Member
- Joined
- Feb 15, 2013
- Messages
- 10
Hi, for a given integer n I perform the following steps until I get to 1
if n is even then then next step is n/2
if n is odd then the next step is 3*n+1
for example.
starting with 3 goes:
3-10-5-16-8-4-2-1
We say that 3 has a length of 8
Now I've written the following code:
Now. My code doesn't work. I just loops around and gives me 5 back again and again and again....
Any ideas as to where the problem is in the code and/or how to fix it?
Thanks,
Tom
if n is even then then next step is n/2
if n is odd then the next step is 3*n+1
for example.
starting with 3 goes:
3-10-5-16-8-4-2-1
We say that 3 has a length of 8
Now I've written the following code:
Code:
Sub problem_14()
x = 1
k = 5
Do Until k = 1
Cells(x, 15) = k
If k = 0 Mod 2 Then
k = k / 2
else
k = k * 3 + 1
End If
x = x + 1
Loop
Cells(4, 4) = x
End Sub
Now. My code doesn't work. I just loops around and gives me 5 back again and again and again....
Any ideas as to where the problem is in the code and/or how to fix it?
Thanks,
Tom