blob: 1f34dccb6d1a3f63907ec831927f47afa32733f4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#!/usr/bin/ruby
def is_prime(n)
if n <= 2
return true
elsif n.modulo(2) == 0
return false
else
3.step(Math.sqrt(n).ceil, 2) do |i|
if n.modulo(i) == 0
return false
end
end
return true
end
end
if ARGV.length < 1
puts "Usage: isprime number [number...]"
else
ARGV.each do |n|
if n.to_i < 1
puts "Negative numbers and 0 not allowed."
exit
elsif is_prime(n.to_i)
puts n.to_s + " is prime"
else
puts n.to_s + " is not prime"
end
end
end
|