This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> function iterative_factorial(n) | |
result = big(1) | |
for i in range(1, n) | |
result *= i | |
end | |
result | |
end | |
iterative_factorial (generic function with 1 method) | |
julia> iterative_factorial(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> function recursive_factorial(n) | |
# first case (base) | |
if n == 0 | |
return 1 | |
end | |
# second case (recursive) | |
if n >= 1 | |
return n*factorial(big(n - 1)) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> function ispalindrome(S::String) | |
i1 = 1 | |
i2 = length(S) | |
while i2 > i1 | |
if S[i1] != S[i2] | |
println(S[i1]) | |
println(S[i2]) | |
return false # `false` is not a variable, that's why `return` is necessary | |
end | |
i1 += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exponentiation by squaring is the standard method for modular exponentiation on big numbers in asymmetric cryptography | |
julia> function expbysquare(b::Int, e::Int) | |
result = BigInt(1) | |
bigb = BigInt(b) | |
while e > 0 | |
if isodd(e) | |
result *= bigb | |
end | |
e >>= 1 # set e to itself shifted by one bit to the right, evaluate the new e after shift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> function hanoi(n, source, dest, spare) | |
if n == 1 | |
println("Move disk 1 from rod $source to rod $dest") | |
else n >= 2 | |
hanoi(n - 1, source, spare, dest) | |
println("Move disk $n from rod $source to rod $dest") | |
hanoi(n - 1, spare, dest, source) | |
end | |
end | |
hanoi (generic function with 1 method) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Julia | |
julia> function quicksort_ex!(A, i=1, j=length(A)) | |
if j > i | |
pivot = A[rand(i:j)] # random element of A | |
left, right = i, j | |
while left <= right | |
while A[left] < pivot # partition | |
left += 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> function insertionsort_ex(A::Array{T}) where T <: Number | |
for i in 1:length(A)-1 | |
value = A[i+1] | |
j = i | |
while value < A[j] && 1 < j # each iteration eliminates one inversion | |
A[j+1] = A[j] # running time depends on number of inversions to eliminate, θ(n+d) | |
j -= 1 # where n = length(A)-1 and d = number of inversions | |
end | |
A[j+1] = value | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Linear regression | |
# part 1: basic function, A = eye(5), trivial | |
# part 2: plotting | |
julia> using CSV | |
julia> data = CSV.read("/Users/kevinliu/Downloads/machine-learning-ex1/ex1/ex1data1.txt", datarow=1) | |
97×2 DataFrames.DataFrame | |
│ Row │ Column1 │ Column2 │ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Linear regression | |
# part 3b: plotting linear fit | |
using Plots, CSV | |
data = CSV.read("/Users/kevinliu/Downloads/machine-learning-ex1/ex1/ex1data1.txt", datarow=1) # or data = rand(97,2) | |
m = length(data[:, 2]) | |
X = hcat(ones(m, 1), data[:, 1]); | |
y = data[:, 2] | |
#thetasA = [-4.090664703327588; -2.386107508525324]; # incorrect when compared to scatter() | |
thetasB = [0.4559997241594341; 2.786203914586563]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Visualize the data | |
using CSV, Plots; pyplot(); | |
data = CSV.read("/Users/kevinliu/Documents/machine-learning-ex2/ex2/ex2data1.txt", datarow=1) | |
X = data[:, [1,2]]; y = data[:, 3]; | |
pos = find(y); neg = find(iszero, y); # or neg = find(t -> t == 0, y); | |
scatter(xaxis=("exam 1 score", (30,100), 30:10:100)) | |
scatter!(yaxis=("exam 2 score", (30,100), 30:10:100)) | |
scatter!(X[pos, 1], X[pos, 2], markershape=:+, label="admitted") |