一些Julia 0.4版中有,被高版本廢棄的功能

lt發表於2017-05-17

1.判斷是否質數、生成質數

julia> isprime(12)
false

julia> primes(12)
5-element Array{Int32,1}:
  2
  3
  5
  7
 11

2.排列組合
permutations和combinations函式返回的是迭代器,要用collect 或自己編寫程式碼顯示內容。

julia> permutations([1 2 3])
Base.Permutations{Array{Int32,2}}(1x3 Array{Int32,2}:
 1  2  3)

julia> permutations(3)
Base.Permutations{Int32}(3)

julia> collect(permutations([1,2,3]))
6-element Array{Array{Int32,1},1}:
 [1,2,3]
 [1,3,2]
 [2,1,3]
 [2,3,1]
 [3,1,2]
 [3,2,1]

julia> collect(combinations([1,2,3],2))
3-element Array{Array{Int32,1},1}:
 [1,2]
 [1,3]
 [2,3]

julia> it1= permutations([1,2,3]);

julia> for i in it1 println(i) end
[1,2,3]
[1,3,2]
[2,1,3]
[2,3,1]
[3,1,2]
[3,2,1]

3.把字串字面量轉化為整數

julia> BigInt("23472387523575783957397395637896")
WARNING: BigInt(s::AbstractString) is deprecated, use parse(BigInt,s) instead.
 in depwarn at deprecated.jl:73
while loading no file, in expression starting on line 0
23472387523575783957397395637896

要在高版本中用這些功能,有些只要包含相應的jl檔案,有些還不行。

julia>include("D:\\Julia-0.4.7\\share\\julia\\base\\primes.jl")
julia> primes(12)
5-element Array{Int32,1}:
  2
  3
  5
  7
 11

julia> isprime(12)
false

julia> include("D:\\Julia-0.4.7\\share\\julia\\base\\combinatorics.jl")
parity (generic function with 1 method)

julia> collect(permutations([1,2,3]))
ERROR: MethodError: no method matching length(::Permutations{Array{Int32,1}})

julia> permutations([1,2,3])
Permutations{Array{Int32,1}}([1, 2, 3])

julia> it1= permutations([1,2,3]);

julia> for i in it1 println(i) end
ERROR: MethodError: no method matching start(::Permutations{Array{Int32,1}})

相關文章