Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Prime Number in Julia #4480

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions archive/j/julia/prime-number.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function err()
return "Usage: please input a non-negative integer"
end


function isPrime(num)
# handle 0 and 1
if num <= 1
return "composite"
end

# default handler
for i in 2:sqrt(num) #loop starting at 2, ending at the squareroot of the input
if (num % i == 0)
return "composite"
end
end
return "prime"
end

#check for a valid input
try
n = parse(Int, ARGS[1])
if n < 0
println(err())
else
println(isPrime(n))
end
catch e
println(err())
end