该代码实现了一个Pollard rho()函数的示例,用于查找正整数n的因子.我已经检查了Julia"Primes"包中的一些代码,这些代码快速运行以试图加速pollard_rho()函数,但都无济于事.代码应该在大约100毫秒到30秒(Erlang,Haskell,Mercury,SWI Prolog)中执行n = 1524157897241274137,但在JuliaBox,IJulia和Julia REPL上需要大约3到4分钟.我怎样才能让它变得快速?
pollard_rho(1524157897241274137)= 1234567891
__precompile__()
module Pollard
export pollard_rho
function pollard_rho{T<:Integer}(n::T)
f(x::T, r::T, n) = rem(((x ^ T(2)) + r), n)
r::T = 7; x::T = 2; y::T = 11; y1::T = 11; z::T = 1
while z == 1
x = f(x, r, n)
y1 = f(y, r, n)
y = f(y1, r, n)
z = gcd(n, abs(x - y))
end
z >= n ? "error" : z
end
end # module
Run Code Online (Sandbox Code Playgroud) 使用 Python 3.4。试图从 IF 语句中调用返回布尔值的函数 prime/2——总是 True 或 False。该函数运行起来很昂贵,所以我只想在我知道需要它时才调用它,因此从决策点内调用。被调用的函数不能可靠地返回 True/False。有时返回值为 None,此时测试失败。我使用 Python 的 IDLE 及其调试器。我调用 primes(2, 5, []) 并逐步执行代码。当 prime/2 到达行 elif n <= p 而 n = 5 和 p = 5 时,调试器显示 prime/2 返回 True,正如它应该的那样,但 primes/3 elif prime(m, 2) 中的行需要一个None 的值。那时我的测试失败了。我的代码:
def primes(m, n, l): # the calling function
if m > n: # creates a list of primes from
print(l, end='\n') # m through n, inclusive
elif m < 2:
primes(2, n, l)
elif m == 2: …Run Code Online (Sandbox Code Playgroud) python functional-programming if-statement function python-3.x