Some solutions for the julia path
This commit is contained in:
23
julia/collatz-conjecture/collatz-conjecture.jl
Normal file
23
julia/collatz-conjecture/collatz-conjecture.jl
Normal file
@@ -0,0 +1,23 @@
|
||||
function is_even(n::Int)
|
||||
return n % 2 == 0
|
||||
end
|
||||
|
||||
function is_odd(n::Int)
|
||||
return n % 2 != 0
|
||||
end
|
||||
|
||||
function collatz_steps(n::Int)
|
||||
if n <= 0
|
||||
throw(DomainError("Input must be a positive integer."))
|
||||
end
|
||||
steps = 0
|
||||
while n != 1
|
||||
if is_even(n)
|
||||
n ÷= 2
|
||||
else
|
||||
n = 3 * n + 1
|
||||
end
|
||||
steps += 1
|
||||
end
|
||||
return steps
|
||||
end
|
||||
Reference in New Issue
Block a user