From ac0f5eb7ebb98c19220b1ed7044c282d72b72845 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 26 Apr 2009 13:27:56 +0200 Subject: Added isprime script --- Makefile | 2 +- isprime | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100755 isprime diff --git a/Makefile b/Makefile index afa20d6..b207c9f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ BINDIR = $(HOME)/bin -TARGETS = mkheader.py +TARGETS = mkheader.py isprime all: $(TARGETS) @echo "Nothing to be done" diff --git a/isprime b/isprime new file mode 100755 index 0000000..1f34dcc --- /dev/null +++ b/isprime @@ -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 -- cgit v1.2.3-54-g00ecf