diff options
-rw-r--r-- | Makefile | 2 | ||||
-rwxr-xr-x | isprime | 31 |
2 files changed, 32 insertions, 1 deletions
@@ -1,6 +1,6 @@ BINDIR = $(HOME)/bin -TARGETS = mkheader.py +TARGETS = mkheader.py isprime all: $(TARGETS) @echo "Nothing to be done" @@ -0,0 +1,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 |