CODE CONJETURE COLLATZ

ZLOSADA

New Member
Joined
Apr 4, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Create a function called "CollatzMaxTotal" 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 that given a number:
If the number is not a positive integer, generate a text that says: "The number is not a positive integer", but if it is a positive integer, generate a text that says the maximum value that the number reached with the Collatz conjecture and the number of steps it takes to get to number 1 for the first time. Example:
If the number is 7 then the function outputs: The maximum value was 52 and to get to 1 it takes 16 steps.
If the number is 9.2 then the function outputs: The number is not a positive integer
If the number is 35 then the function outputs: The maximum value was 160 and it takes 13 steps to get to 1.
Thank you
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello. Interesting conjeture:

This is an array function that occupies two cells.
If you don't know how to incorporate it into your sheet, please comment.

PHP:
Function CollatzMaxTotal(n)
Dim i&, j&, M&, v As Boolean
Rem ----------------------->
CollatzMaxTotal = Array("Wrong", "number")
If Not IsNumeric(n) Then Exit Function
If n < 1 Or n <> Int(n) Then Exit Function
Rem ----------------------->
j = n: M = j
Do While j <> 1
  If (j Mod 2) = 0 Then j = j / 2 Else j = 3 * j + 1
  M = Application.Max(M, j): i = 1 + i
Loop
Rem ----------------------->
CollatzMaxTotal = Array(M, i)
End Function

Test_1.xlsm
ABC
1NMaxStep
2110
3221
43167
5442
65165
76168
875216
9883
1095219
1110166
Hoja1
Cell Formulas
RangeFormula
B2:C11B2=CollatzMaxTotal(A2)
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 1

Forum statistics

Threads
1,215,372
Messages
6,124,537
Members
449,169
Latest member
mm424

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