Wolfram MathworldのKempner Sereisの項には,ケンプナー級数の具体的な数値が与えられている。というわけで,さっそくJuliaを使って計算してみよう。そうそう,いつの間にか,Juliaは1.2にバージョンアップしている。再帰を使ったコードは次のとおり。
上述のMathworldでは,9を除去した場合のケンプナー級数の収束値が22.92になっているので,10^10個までの範囲でも7割程度の値しか得られていない。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function core(is,m,x,sum)
# local e,y::BigFloat
e=1
is=is-1
for k in 1:9
y=10*x+m[k]
if(y!=0 && is==0)
sum=sum+e/y
# println(y)
end
if(is>0)
sum=core(is,m,y,sum)
end
end
is=is+1
return sum
end
function kempner(is,id)
#is : depth of recursive call core
#id : figure # to remove from sum
# local e,a,sum::BigFloat
e=1
m=[0,1,2,3,4,5,6,7,8,9]
m=deleteat!(m,id+1)
sum=0
for i in 1:9
a=m[i]
sum=core(is,m,a,sum)
end
println(sum)
end
for is in 1:10
println(is)
@time kempner(is,9)
end
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1
4.77184876508206
0.000109 seconds (60 allocations: 1.344 KiB)
2
6.589720190283038
0.000105 seconds (61 allocations: 1.813 KiB)
3
8.223084402866208
0.000134 seconds (60 allocations: 1.641 KiB)
4
9.692867792106203
0.000408 seconds (60 allocations: 1.641 KiB)
5
11.015650849872554
0.002865 seconds (59 allocations: 1.313 KiB)
6
12.206153622565859
0.024687 seconds (60 allocations: 1.641 KiB)
7
13.277605939858102
0.216985 seconds (212 allocations: 12.234 KiB)
8
14.2419130093833
1.947383 seconds (210 allocations: 10.734 KiB)
9
15.109789370852864
17.840434 seconds (211 allocations: 11.078 KiB)
10
15.890878115347133
160.770402 seconds (210 allocations: 10.750 KiB)
比較のため,単純な調和級数の計算コードと所要時間をあげておく。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function test(n)
e=1
m=10^n
sum=0
for i in 1:m
sum=sum+e/i
end
println(sum)
end
for i in 1:10
println(i)
@time test(i)
end
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 2.9289682539682538 0.000078 seconds (60 allocations: 1.531 KiB) 2 5.187377517639621 0.000071 seconds (59 allocations: 1.344 KiB) 3 7.485470860550343 0.000075 seconds (58 allocations: 1.156 KiB) 4 9.787606036044348 0.000158 seconds (58 allocations: 1.156 KiB) 5 12.090146129863335 0.000668 seconds (58 allocations: 1.156 KiB) 6 14.392726722864989 0.005231 seconds (58 allocations: 1.156 KiB) 7 16.695311365857272 0.050618 seconds (58 allocations: 1.156 KiB) 8 18.997896413852555 0.520200 seconds (212 allocations: 13.266 KiB) 9 21.30048150234855 5.296834 seconds (210 allocations: 10.906 KiB) 10 23.6030665949975 53.307042 seconds (209 allocations: 10.578 KiB)