while loop to count steps of inner loops

Itsi256

New Member
Joined
Sep 1, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am writing a Sub to identify the longest Collatzs Sequenze between 1 and 1000. How do I need to implement an inner while loop to count the steps of the inner loops to identify the longest Collatz sequence?

Sub LongestCollatz()

VBA Code:
Dim i As Long
Dim maxSteps As Integer


    For i = 1 To 1000
    
        If i = 1 Then
            maxSteps = CInt(20.856 * Log(2))
        Else
            maxSteps = CInt(20.856 * Log(i))
        End If
    
        If i Mod 2 = 0 Then
            i = i / 2
        Else
            i = 3 * i + 1
        End If
    
    Debug.Print i
        
            
        End If
        
                 
    Next i
    
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
It is not a good practice to modify a For loop iterator inside the loop. This gives i a repeating sequence of 4, 2, 1.... In this case i will never reach 1000 and you have an infinite loop.

You have an unmatched End If.

You are setting but not using maxSteps.

I can see some logic that relates to the the Collatz conjecture but don't really understand your overall approach.

Your question is unclear, because there is no inner loop. I am guessing that you want to count the number of iterations that occur. However, you need to determine what is the condition to exit the loop.
 
Upvote 0
Thank you very much for your recommendation and sorry for the confusion.
Yes in the Code is no inner loop, because I don't know how to do it...this is why am asking. But it seems my question is not clear so is it possible to delete my question and write in a more clear way?
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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