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
thank you for any help
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: