Julia
Julia
Installation
Ubuntu
apt install julia
Arch Linux
pacman -S julia
Binary
# Download julia-{version}-{os}-{arch}.tar.gz from https://julialang.org
tar xzf julia-{version}-{os}-{arch}.tar.gz
mv julia-{version} /usr/local
export PATH=/usr/local/julia-{version}/bin:$PATH
git
git clone https://github.com/JuliaLang/julia.git
cd julia
make
mkdir /usr/local/julia-{version}
cp -r usr/* /usr/local/julia-{version}
export PATH=/usr/local/julia-{version}/bin:$PATH
Examples
Fibonacci Numbers
# Fibonacci Numbers
function fib(n)
if n < 2
return n
else
return fib(n - 2) + fib(n - 1)
end
end
n = parse(Int32, ARGS[1])
println(fib(n))
#=
example
julia -O -- fib.jl 39
=#
just the way julia is
# Fibonacci Numbers
fib(n) = begin
n < 2 && return n
fib(n - 2) + fib(n - 1)
end
n = parse(Int32, ARGS[1])
println(fib(n))
#=
example
julia -O -- fib.jl 39
=#
Matrix form
# Fibonacci Numbers
fib(n) = ([1 1;1 0]^n)[1,2]
n = parse(Int32, ARGS[1])
println(fib(n))
#=
example
julia -O -- fib_matrix.jl 39
=#
Binet's formula
# Fibonacci Numbers
using Base.MathConstants
fib(n) = floor(Int, φ ^ n / √5 + 0.5)
n = parse(Int32, ARGS[1])
println(fib(n))
#=
example
julia -O -- fib_binets.jl 39
=#