我在划分大数时遇到问题。我有一个 while 循环,如果索引在数组限制内并且其余数为零,则将数字除以:
while ((i + j*arr[i]) <= lim && arr[i + j*arr[i]] % arr[i] == 0)
arr[i + j*arr[i]] /= arr[i]
end
Run Code Online (Sandbox Code Playgroud)
我在最大值为 Int64 的数组上使用arr[i]<(150*10^6)^2
,即使更改为 Int128 也无济于事,BigInt 是不可能的,因为它需要太多时间。我不明白为什么只有在余数为零时才进入循环,我会在循环内得到 Inexact 错误。而typemax(Int64)>max(arr)
这里是完整的错误:
InexactError: Int64(1.8014400929875968e15)
in top-level scope at P-146-Investigating a Prime Pattern:51
in test at P-146-Investigating a Prime Pattern:39
in setindex! at base\array.jl:826
in convert at base\number.jl:7
in Int64 at base\float.jl:710
Run Code Online (Sandbox Code Playgroud)
似乎只有在数组中的值高于之后才会发生 (90*10^6)^2
我已经用进度条做了一些测试,它大大减慢了测试代码的速度。有没有替代方案或解决方案?我正在寻找一种在循环时跟踪当前索引的方法,并且有一些原始方法可以在到达步骤时放置更多条件进行打印,但不是有内置的好东西吗?哦,还有一个问题,有没有办法打印从函数启动开始经过的时间并与索引一起显示?让我澄清一下,我知道@time 等,但是有没有办法计算时间并用相应的索引显示它,例如
"Reached index $i in iteration in time $time"
完成测试的代码:
function test(x)
summ = BigInt(0);
Juno.progress(name = "foo") do id
for i = 1:x
summ+=i;
@info "foo" progress=i/x _id=id
end
end
println("sum up to $x is $summ");
return summ;
end
@benchmark test(10^4)
function test2(x)
summ = BigInt(0);
for i = 1:x
summ+=i;
(i%10 == 0) && println("Reached this milestone $i")
end
println("sum up to $x is $summ");
return summ;
end
@benchmark test2(10^4)
Run Code Online (Sandbox Code Playgroud)
编辑 1
对于 Juno.progress:
BenchmarkTools.Trial:
memory estimate: …
Run Code Online (Sandbox Code Playgroud) julia ×2