Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active August 29, 2015 14:11
Show Gist options
  • Save komasaru/49ca44157469ba528563 to your computer and use it in GitHub Desktop.
Save komasaru/49ca44157469ba528563 to your computer and use it in GitHub Desktop.
Ruby script to list prime numbers.
#! /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