我一直试图在Julia中为斐波那契函数做记忆.这就是我提出的.
原始未修改代码(用于控制目的)
function fib(x)
if x < 3
return 1
else
return fib(x-2) + fib(x-1)
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的第一次尝试
memory=Dict()
function memfib(x)
global memory
if haskey(memory,x)
return memory[x]
else
if x < 3
return memory[x] = 1
else
return memory[x] = memfib(x-2) + memfib(x-1)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的第二次尝试
memory=Dict()
function membetafib(x)
global memory
return haskey(memory,x) ? memory[x] : x < 3 ? memory[x]=1 : memory[x] = membetafib(x-2) + membetafib(x-1)
end
Run Code Online (Sandbox Code Playgroud)
我的第三次尝试
memory=Dict()
function memgammafib!(memory,x)
return haskey(memory,x) ? memory[x] : x < …Run Code Online (Sandbox Code Playgroud) 我想对复数的列表(或julia说一维数组)进行排序,通过实数,然后是复数的虚部.我尝试使用匿名函数,但它不起作用.
julia> b=[3 + 1im,1 + 2im,1 + 1im,5 + 6im]
4-element Array{Complex{Int64},1}:
3 + 1im
1 + 2im
1 + 1im
5 + 6im
julia> sort(b,lt = x,y -> if(real(x)==real(y)) imag(x)<imag(y) else real(x)<real(y) end)
ERROR: UndefVarError: x not defined
Stacktrace:
[1] top-level scope at none:0
Run Code Online (Sandbox Code Playgroud)
我想要以下结果
1 + 1im
1 + 2im
3 + 1im
5 + 6im
Run Code Online (Sandbox Code Playgroud) 当我试图计算时
julia> -2.3^-7.6
-0.0017818389423254909
Run Code Online (Sandbox Code Playgroud)
但是我的计算器给出的结果是
0.0005506 + 0.001694 i
Run Code Online (Sandbox Code Playgroud)
为了安全起见,我再次尝试了,这次它抱怨了.我第一次尝试时为什么不抱怨?
julia> a = -2.3; b = -7.6; a^b
ERROR: DomainError with -2.6:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
Stacktrace:
[1] throw_exp_domainerror(::Float64) at ./math.jl:35
[2] ^(::Float64, ::Float64) at ./math.jl:769
[3] top-level scope at none:0
[4] eval at ./boot.jl:319 [inlined]
[5] #85 at /Users/ssiew/.julia/packages/Atom/jodeb/src/repl.jl:129 [inlined]
[6] with_logstate(::getfield(Main, Symbol("##85#87")),::Base.CoreLogging.LogState) at ./logging.jl:397
[7] with_logger(::Function, ::Atom.Progress.JunoProgressLogger) at ./logging.jl:493
[8] top-level scope at /Users/ssiew/.julia/packages/Atom/jodeb/src/repl.jl:128
Run Code Online (Sandbox Code Playgroud) 在 vi 程序中,我可以通过以下方式输入 unicode
首先使用“i”进入“插入模式”
然后输入 'CTRL-v' 后跟 'u2611' 以输入 unicode-2611
我想为“julia 1.0 Integer Div symbol”输入unicode字符
但是我不知道它的 unicode 值是什么,当我查看 julia 1.0 文档时,它没有告诉我
https://docs.julialang.org/en/v1.0.0/manual/mathematical-operations/
julia 1.0 Integer Div 符号的 unicode 是什么?
function main()
(k, a, b, a1, b1) = (BigInt(2), BigInt(4), BigInt(1), BigInt(12), BigInt(4))
while true
(p, q, k) = (k*k, BigInt(2)*k+BigInt(1), k+BigInt(1))
(a, b, a1, b1) = (a1, b1, p*a+q*a1, p*b+q*b1)
(d,d1) = ( div(a,b),div(a1,b1) )
while d == d1
write(STDOUT,string(d))
(a,a1) = ( BigInt(10) * (a % b), BigInt(10) * (a1 % b1) )
(d,d1) = ( div(a,b),div(a1,b1) )
end
end
end
main()
Run Code Online (Sandbox Code Playgroud)
当我编译它时,它给我错误消息
$ julia pi_2.jl
ERROR: LoadError: UndefVarError: STDOUT not defined
Run Code Online (Sandbox Code Playgroud)
它在julia 0.6中完美运行并生成了Pi的所有数字
$ julia06 pi_2.jl
3141592653589793238462643383279502884197169399375105820974944592307816...
Run Code Online (Sandbox Code Playgroud)