1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VBAでコラッツ逆計算を二次元配列として書き出し、Excelの表にするコード

Last updated at Posted at 2025-03-30

コラッツ予想の計算において、1から逆にとらえた計算の奇数の遷移に着目

コラッツ予想の計算を逆にとり、その奇数のみの数列をを二次元配列として書き出します。
計算式は、コラッツ計算の漸化式から一般項を求めました。
6で割ったときに1余るか5余るかで、2で割る回数が、偶数か奇数かが変わるので、一般項は2つ存在します。【1*】【2*】

excelマクロ
Sub コラッツ奇数配列()
    Dim s As Integer, t As Integer, n As Integer, m As Integer  's,t,n,mの宣言
    Dim 奇数_e(100, 100) As Variant '奇数_eの二次元配列の宣言
    Dim 奇数_o(100, 100) As Variant '奇数_oの二次元配列の宣言
    
    n = 20  '表示したい列数
    m = 20  '表示したい行数
    
    Range(Cells(1, 1), Cells(2, n)).Font.Color = RGB(255, 0, 0)  '参照奇数の二行の文字色設定
    Range(Cells(1, 1), Cells(1, n)).Interior.Color = RGB(226, 226, 226)  '参照奇数_eの行の背景色設定
    
    For t = 1 To n  '列を指定
    
        奇数_e(0, t) = 6 * t - 5  '参照奇数_eを求める式(一行目)
        奇数_o(0, t) = 6 * t - 1  '参照奇数_oを求める式(二行目)
        Cells(1, t).Value = 奇数_e(0, t)  '参照奇数_eをセルに入力(一行目)
        Cells(2, t).Value = 奇数_o(0, t)  '参照奇数_oをセルに入力(二行目)
    
        For s = 1 To m / 2 '行を指定
            
            奇数_e(s, t) = (4 * 4 ^ (s - 1) - 1) / 3 + (t - 1) * 8 * 4 ^ (s - 1)  '奇数_eを求める式【1*】
            奇数_o(s, t) = (10 * 4 ^ (s - 1) - 1) / 3 + (t - 1) * 4 * 4 ^ (s - 1)  '奇数_oを求める式【2*】
            Cells(s * 2 + 1, t).Value = 奇数_e(s, t)  '奇数_eをセルに入力
            Cells(2 * s + 2, t).Value = 奇数_o(s, t)  '奇数_oをセルに入力
            Range(Cells(s * 2 + 1, t), Cells(s * 2 + 1, t)).Interior.Color = RGB(226, 226, 226)  '奇数_eの行の背景色設定(一行ごとに着色)
            
        Next s
    Next t
     
End Sub

このコードを実行すると、以下のように書き出せます。

excel_draw_e_table.jpg

奇数のみを考えると、
1 → {1, 5, 21, 85, 341,…}
5 → {3, 13, 53, 213, 853,…}
7 → {9, 37, 149, 597, 2389,…}
11 → {7, 29, 117, 469, 1877,…}
13 → {17, 69, 277, 1109, 4437,…}
17 → {11, 45, 181, 725, 2901,…}
のように遷移するわけですが、この{}の奇数の並びが、書き出したExcelでは縦の行のひとつ飛ばしになります。

このコードでは20×20(n=20,m=20)しか表してないですが、【1*】【2*】この二つの式で正の奇数が重複なくもれなく表わせます。
Excelでマクロに貼り付けて、実行させるだけ…数学的な証明など抜きにして、コラッツ予想が真であることを体感していただけるかと思います。

ご覧いただき、ありがとうございました!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?