trouble with do loop

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:


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
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Tom

It should be k Mod 2 = 0, not k=0 Mod 2
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top