ari's world

あるかどうかわからないけど、あるみたい。ありがとう。

Collaz 予想・Windows バッチ版

とっくに期限は過ぎているのですけれども、「キミならどう書く 2.0 - ROUND 2 -」の Collaz 予想Windows バッチでやってみました。先日のアスタリスク・グラフ形式で出力するようにし、答えは目視で探します(笑・もちろん先日 max を実装しているのでそのまま出してもよかったのですけれど、つまらないので)。キャッシュを使って再利用すると劇的に早くなりそうだけど、とりあえずスピードアップなども考えずまじめにこりこりやっているため遅いです。

f.cmd

f(n) を計算


@echo off

if %1 equ 1 (
set /A f = %1
goto finish_point
)

set /A mod_even = %1 %% 2

if %mod_even% EQU 0 (
set /A f = %1/2
) else (
set /A f = 3*%1 + 1
)
set mod_even=

:finish_point

g.cmd

g(n) を計算。


@echo off

set g=1

set f=%1

:f_again

call "%~dp0f.cmd" %f%
set /A g = %g% + 1
if %f% neq 1 (
goto f_again
)

collatz_graph.cmd

グラフを出力


@echo off
setlocal

set count=%1

:start_point
call %~dp0g.cmd %count%
echo %count% %g%
set /A count = %count% - 1
if %count% neq 0 (
goto start_point
)

実行と結果


D:\collatz>collatz_graph.cmd 100 > collatz_graph.txt

D:\asteriskgrap>graph.cmd collatz_graph.txt
100 :************* [26]
99 :************* [26]
98 :************* [26]
97 :*********************************************************** [119]
96 :****** [13]
95 :***************************************************** [106]
94 :***************************************************** [106]
93 :********* [18]
92 :********* [18]
91 :********************************************** [93]
90 :********* [18]
89 :*************** [31]
88 :********* [18]
87 :*************** [31]
86 :*************** [31]
85 :***** [10]
84 :***** [10]
83 :******************************************************* [111]
82 :******************************************************* [111]
81 :*********** [23]
80 :***** [10]
79 :****************** [36]
78 :****************** [36]
77 :*********** [23]
76 :*********** [23]
75 :******* [15]
74 :*********** [23]
73 :********************************************************** [116]
72 :*********** [23]
71 :*************************************************** [103]
70 :******* [15]
69 :******* [15]
68 :******* [15]
67 :************** [28]
66 :************** [28]
65 :************** [28]
64 :*** [7]
63 :****************************************************** [108]
62 :****************************************************** [108]
61 :********** [20]
60 :********** [20]
59 :**************** [33]
58 :********** [20]
57 :**************** [33]
56 :********** [20]
55 :******************************************************** [113]
54 :******************************************************** [113]
53 :****** [12]
52 :****** [12]
51 :************ [25]
50 :************ [25]
49 :************ [25]
48 :****** [12]
47 :**************************************************** [105]
45 :******** [17]
44 :******** [17]
43 :*************** [30]
42 :**** [9]
41 :******************************************************* [110]
40 :**** [9]
39 :***************** [35]
38 :*********** [22]
37 :*********** [22]
36 :*********** [22]
35 :******* [14]
34 :******* [14]
33 :************* [27]
32 :*** [6]
31 :***************************************************** [107]
30 :********* [19]
29 :********* [19]
28 :********* [19]
27 :******************************************************** [112]
26 :***** [11]
25 :************ [24]
24 :***** [11]
23 :******** [16]
22 :******** [16]
21 :**** [8]
20 :**** [8]
19 :********** [21]
18 :********** [21]
17 :****** [13]
16 :** [5]
15 :********* [18]
14 :********* [18]
13 :***** [10]
12 :***** [10]
11 :******* [15]
10 :*** [7]
9 :********** [20]
8 :** [4]
7 :******** [17]
6 :**** [9]
5 :*** [6]
4 :* [3]
3 :**** [8]
2 :* [2]
1 :* [2]

よって h(100) については g(97)=119。


これって正しいのかな?