anonymous No title
No License VBA
2020年11月08日
Copy Clone
Option Explicit

Sub main()
    Cells.ClearContents
    
    Dim tryMin As Variant: tryMin = 1 '試行数値の最初の数値
    Dim tryTimes As Long: tryTimes = 1000 'tyrMinから何番目まで計算したいか。※上限16384(Excelの最大列数)
    
    Dim culcTimesCapa As Long: culcTimesCapa = 1000 '計算回数許容範囲
    
    
    Range("A1").Resize(culcTimesCapa + 2, tryTimes) = CollatzFullVertical(tryMin, tryTimes, culcTimesCapa)
                
End Sub

'計算過程:行方向へ出力
Function CollatzFullVertical( _
    ByVal tryMin As Variant, _
    ByVal tryTimes As Long, _
    Optional ByVal culcTimesCapa As Long) _
    As Variant
    
    If culcTimesCapa = 0 Then culcTimesCapa = 1000
    
    Dim tryMax As Variant: tryMax = tryMin + tryTimes - 1
    ReDim arr(culcTimesCapa + 1, tryTimes - 1) As Variant
    Dim i As Variant, j As Long
    Dim n As Variant, row As Long, col As Long
    
    
    For i = tryMin To tryMax
        n = i
        row = 1
        arr(row, col) = n
        Do While n <> 1
            If Right(n, 1) Mod 2 = 0 Then 'Rigth関数はオーバーフロー対策
                n = n / 2
            Else
                n = n * 3 + 1
            End If
            row = row + 1
            arr(row, col) = n
        Loop
        arr(0, col) = row - 1 & "回"
        'このループはスピルで使用する場合はあった方が良い。スピルで使用しない場合はなくても良い
        For j = row + 1 To culcTimesCapa + 1
            arr(j, col) = ""
        Next
        '--------------------------------------------
        col = col + 1
    Next
    CollatzFullVertical = arr
End Function


Option Explicit

Sub main()
    Cells.ClearContents
    
    Dim tryMin As Variant: tryMin = 1 '試行数値の最初の数値
    Dim tryTimes As Long: tryTimes = 1000 'tyrMinから何番目まで計算したいか。※上限16384(Excelの最大列数)
    
    Dim culcTimesCapa As Long: culcTimesCapa = 1000 '計算回数許容範囲
    
    
    Range("A1").Resize(culcTimesCapa + 2, tryTimes) = CollatzFullVertical(tryMin, tryTimes, culcTimesCapa)
                
End Sub

'計算過程:行方向へ出力
Function CollatzFullVertical( _
    ByVal tryMin As Variant, _
    ByVal tryTimes As Long, _
    Optional ByVal culcTimesCapa As Long) _
    As Variant
    
    If culcTimesCapa = 0 Then culcTimesCapa = 1000
    
    Dim tryMax As Variant: tryMax = tryMin + tryTimes - 1
    ReDim arr(culcTimesCapa + 1, tryTimes - 1) As Variant
    Dim i As Variant, j As Long
    Dim n As Variant, row As Long, col As Long
    
    
    For i = tryMin To tryMax
        n = i
        row = 1
        arr(row, col) = n
        Do While n <> 1
            If Right(n, 1) Mod 2 = 0 Then 'Rigth関数はオーバーフロー対策
                n = n / 2
            Else
                n = n * 3 + 1
            End If
            row = row + 1
            arr(row, col) = n
        Loop
        arr(0, col) = row - 1 & "回"
        'このループはスピルで使用する場合はあった方が良い。スピルで使用しない場合はなくても良い
        For j = row + 1 To culcTimesCapa + 1
            arr(j, col) = ""
        Next
        '--------------------------------------------
        col = col + 1
    Next
    CollatzFullVertical = arr
End Function


No one still commented. Please first comment.