23 lines
420 B
Text
23 lines
420 B
Text
def factorial(n)
|
|
"""Calculate factorial recursively"""
|
|
if n <= 1
|
|
return 1
|
|
else
|
|
return n * factorial(n - 1)
|
|
end
|
|
end
|
|
def is_prime(num)
|
|
if num < 2
|
|
return false
|
|
end
|
|
for i in 2...Integer(num ** 0.5) + 1
|
|
if num % i == 0
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
print("Factorial of 5: #{factorial(5)}", "\n")
|
|
for n in [2, 3, 4, 17, 21]
|
|
print("#{n} is prime: #{is_prime(n)}", "\n")
|
|
end
|