Home » excel » excel – Collatz conjecture. Find the smallest a_k

excel – Collatz conjecture. Find the smallest a_k

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to create a function, that will return the smallest a_k, such that a_k equal to one. I wrote this code:

Function Collatz(N As Integer) As Integer

    If N Mod 2 = 0 Then
        F = N / 2
    Else
        F = (3 * N) + 1
    End If

    Collatz = F

End Function



Function Find(N As Integer) As Integer
 k = 0
 Do While N <> 1
    N = Collatz(N): k = k + 1
    If k > 10000000000# Then Exit Do
 Loop

End Function

Function Collatz works fine, but function Find always returns zero. How can I improve this?

How to&Answers:

you forgot to return the result.

Find = k