Last active
July 20, 2019 22:06
-
-
Save adityasuseno/913d9b29739d51b31a7c19d9f5320232 to your computer and use it in GitHub Desktop.
Simple program in Julia to check whether a number is prime or not
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
function isPrime(n) | |
if n < 2 | |
return false | |
elseif n == 2 | |
return true | |
else | |
for i in 2:n-1 | |
p = n % i | |
if p == 0 | |
return false | |
break | |
else | |
continue | |
end | |
end | |
return true | |
end | |
end | |
while true | |
try | |
print("Tampilkan Banyaknya Bilangan Prima antara 1 hingga: ") | |
x = parse(Int32, readline()) | |
let | |
global y = 0 | |
for x in 2:x | |
if isPrime(x) == true | |
y += 1 | |
end | |
end | |
end | |
println(y) | |
break | |
catch | |
println("Harap Masukan Bilangan Bulat saja!") | |
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
function isPrime(n) | |
if n < 2 | |
return false | |
elseif n == 2 | |
return true | |
else | |
for i in 2:n-1 | |
p = n % i | |
if p == 0 | |
return false | |
break | |
else | |
continue | |
end | |
end | |
return true | |
end | |
end | |
print("Enter a number: ") | |
x = parse(Int32, readline()) | |
println(isPrime(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment