diff --git a/Makefile b/Makefile index d12a8648850dadbbe6691d844b980d8a4b4bf101..c77f8b62e2ab9259b78c71d1c3721f1aef3e14d3 100644 --- a/Makefile +++ b/Makefile @@ -14,4 +14,5 @@ test: @ time -f "%C : %E seconds" ./prime.py 100000 > /dev/null @ time -f "%C : %E seconds" ./prime.lua 100000 > /dev/null @ time -f "%C : %E seconds" ./prime.php 100000 > /dev/null + @ time -f "%C : %E seconds" ./prime.rb 100000 > /dev/null @ time -f "%C : %E seconds" ./prime.js 100000 > /dev/null diff --git a/prime.rb b/prime.rb new file mode 100755 index 0000000000000000000000000000000000000000..358647a82e39d1535ba1648e34c8189c7f6b1e49 --- /dev/null +++ b/prime.rb @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +def isPrime(num) + num_sqr = Math.sqrt(num) + + return false if num_sqr % 1 == 0 + + (2..num_sqr.to_i).each do |n| + return false if num % n == 0 + end + + return true +end + +primes = [] +i = 2 + +while primes.count < ARGV.first.to_i do + primes << i if isPrime(i) + i += 1 +end + +puts primes