primes/primes.rb

37 lines
522 B
Ruby
Executable File

#!/usr/bin/env ruby
# Ruby version of a Prime Number Finder
# haplo@mindstab.net
include Math
class Primes
def findPrimes(cmax)
3.step(cmax,2) { |i|
max = sqrt(i)
isPrime = 1
3.step(max, 2) { |x|
isPrime = i % x
if isPrime == 0
break
end
}
if isPrime != 0
print i, "\n"
end
}
end
end
MAX = ARGV[0].to_i;
if ! MAX
print "primes.rb <MAX NUM>\n"
exit(1)
elsif MAX < 1
print "Invalid MAX Input\n"
exit(1)
end
prime = Primes.new();
prime.findPrimes(MAX);