vba Collatz conjecture

Imran Azam

Board Regular
Joined
Mar 15, 2011
Messages
103
Hi Guys

I am trying to create a macro which follows the Collatz Conjecture( basically says: Pick any natural number n. If n is even, take n and divide by 2. If n is odd, multiply n by 3 and add 1. Repeat this algorithm and regardless of what number you picked to begin with, you will always end at 1)

The macros needs to do the following

1) allow the user to type input number via an input box and also type Max number via another input box , both these number have to be positive number , if it isn't a positive number then need a message to appear saying please pick a positive number

2) the macro now needs to take the Collatz Conjecture maths and see if the number of steps taken to get the input number (to equal to1) is less than or equal to the max number inputted , if this is the case then a message box appears showing the the number of steps taken) . If the steps taken are higher than the max number give then return a message saying ' process didn't work'

for example if user insert the input number as 10 and max number as 7 , and this input number ( 10) needs 5 steps ( Collatz Conjecture) to get to the number 1 then a message return would be " 5".
However for example if the input number 10 needed 8 steps to get to the number 1 then message would return ' process didn't work' as this is more than the max number.

can any one help with this?

i did some code but made a mess of it , it isnt really working its shown below

VBA Code:
Sub collat2()
n = InputBox("type in a number ")
am = 0
Do While n <> 1
    If n Mod 2 = 0 Then
        n = n / 2
    Else
        n = n * 3 + 1
    End If
If n > am Then
    Z = am
End If
x = x + 1
Dim ligne As Integer
'Cells(1, 1).Value = "Trip"
Cells(x, 1).Value = n
Loop
'Cells(1, 2).Value = "Steps"
Cells(x, 2).Value = x
'Cells(1, 3).Value = "Maximal altitude"
Cells(x, 3).Value = am
End Sub

thank you for any help
 
Last edited by a moderator:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
What is the final results: Input value => output Message Box (as per you said in words) OR Input value => write steps on sheets (as per your code)? OR both?
 
Upvote 0
Below code write process also send a message box

VBA Code:
Sub collat2()
Dim n, m, i, k As Long
Range("A2:d1000").ClearContents
n = InputBox("Type in a number:")
m = InputBox("Max number:")
If n <= 0 Or m <= 0 Then
MsgBox "Pls pick a positive number"
Exit Sub
End If
For i = 1 To 10000
    Select Case n Mod 2
    Case Is = 0
        n = n / 2
    Case Else
        n = (n * 3 + 1) / 2
    End Select
    Cells(k + 2, 1).Value = n
    Cells(k + 2, 2).Value = k + 1
    If n = 1 Then
        MsgBox "steps = " & k + 1
        If k + 1 > m Then MsgBox "Process didn't work"
    Exit Sub
    End If
    k = k + 1
Next
End Sub
 
Upvote 0
Below code write process also send a message box

VBA Code:
Sub collat2()
Dim n, m, i, k As Long
Range("A2:d1000").ClearContents
n = InputBox("Type in a number:")
m = InputBox("Max number:")
If n <= 0 Or m <= 0 Then
MsgBox "Pls pick a positive number"
Exit Sub
End If
For i = 1 To 10000
    Select Case n Mod 2
    Case Is = 0
        n = n / 2
    Case Else
        n = (n * 3 + 1) / 2
    End Select
    Cells(k + 2, 1).Value = n
    Cells(k + 2, 2).Value = k + 1
    If n = 1 Then
        MsgBox "steps = " & k + 1
        If k + 1 > m Then MsgBox "Process didn't work"
    Exit Sub
    End If
    k = k + 1
Next
End Sub
Hi

Thank you for this, however when i test this code it with positive numbers i keep getting the message ""Please pick a positive number""
 
Upvote 0
With this:
n = InputBox("Type in a number:")
m = InputBox("Max number:")
If n <= 0 Or m <= 0 Then
MsgBox "Pls pick a positive number"

I think no way msgbox is activated with n or m>0

Could you try again, and copy the screenshot?
 
Upvote 0
With this:
n = InputBox("Type in a number:")
m = InputBox("Max number:")
If n <= 0 Or m <= 0 Then
MsgBox "Pls pick a positive number"

I think no way msgbox is activated with n or m>0

Could you try again, and copy the screenshot?
what screenshot do you want me to copy?
 
Upvote 0
Thanks for this the code seems to be working.
Are you sure. For an input of 10 it produces 5 steps of 5,8,4,2,1 when by my understanding (& your original code) it should be 6 steps of 5,16,8,4,2,1

BTW, when posting vba code please use the available code tags. My signature block below has more details. I fixed it for you this time.


can a do while loop be used in this?
You could try the code below. I used an 'Until' loop rather than a 'While'. :)
I am unsure whether you want the intermediate values entered in the worksheet or not. Your description does not mention that but your code does it.
If you do not need the actual values, you can remove the lines marked with '***** in the code below and you would simply get the message box at the end.

VBA Code:
Sub collat2_v2()
  Dim n As Long, MaxTries As Long, k As Long
 
  Columns("A:B").ClearContents          '*****
  Do
    n = InputBox("Enter a positive number to test")
    If n <= 0 Then MsgBox "Number must be positive"
  Loop Until n > 0
  Do
    MaxTries = InputBox("Enter a positive number for maximum number of tries")
    If MaxTries <= 0 Then MsgBox "Number must be positive"
  Loop Until MaxTries > 0
  Cells(1, 1).Value = n                 '*****
  Do Until n = 1 Or k = MaxTries
    n = IIf(n Mod 2, n * 3 + 1, n / 2)
    k = k + 1
    Cells(k + 1, 1) = n                 '*****
  Loop
  Cells(k + 1, 2).Value = k             '*****
  MsgBox IIf(n = 1, "Steps = " & k, "Process didn't work")
End Sub
 
Upvote 0
Are you sure. For an input of 10 it produces 5 steps of 5,8,4,2,1 when by my understanding (& your original code) it should be 6 steps of 5,16,8,4,2,1

BTW, when posting vba code please use the available code tags. My signature block below has more details. I fixed it for you this time.



You could try the code below. I used an 'Until' loop rather than a 'While'. :)
I am unsure whether you want the intermediate values entered in the worksheet or not. Your description does not mention that but your code does it.
If you do not need the actual values, you can remove the lines marked with '***** in the code below and you would simply get the message box at the end.

VBA Code:
Sub collat2_v2()
  Dim n As Long, MaxTries As Long, k As Long
 
  Columns("A:B").ClearContents          '*****
  Do
    n = InputBox("Enter a positive number to test")
    If n <= 0 Then MsgBox "Number must be positive"
  Loop Until n > 0
  Do
    MaxTries = InputBox("Enter a positive number for maximum number of tries")
    If MaxTries <= 0 Then MsgBox "Number must be positive"
  Loop Until MaxTries > 0
  Cells(1, 1).Value = n                 '*****
  Do Until n = 1 Or k = MaxTries
    n = IIf(n Mod 2, n * 3 + 1, n / 2)
    k = k + 1
    Cells(k + 1, 1) = n                 '*****
  Loop
  Cells(k + 1, 2).Value = k             '*****
  MsgBox IIf(n = 1, "Steps = " & k, "Process didn't work")
End Sub
thank you, i dont need the figures posted onto the worksheet
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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