Last active
August 29, 2015 14:11
-
-
Save komasaru/49ca44157469ba528563 to your computer and use it in GitHub Desktop.
Ruby script to list prime numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/ruby | |
# coding: utf-8 | |
# -------------------------------------- | |
# Prime numbers list | |
# -------------------------------------- | |
class PrimeNumber | |
def exec(nums) | |
(1..nums).each do |n| | |
puts "#{n}: #{is_prime(n) ? 'PRIME' : '----- '}" | |
end | |
end | |
private | |
def is_prime(n) | |
res = (2..Math.sqrt(n)).any? { |i| n % i == 0 } | |
return res || n == 1 ? false : true | |
end | |
end | |
PrimeNumber.new.exec(99) if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment