素数日を求める Apr 19th, 2012 blogのpostテストがてら。 #-*- coding:utf-8 -*- require 'date' require 'mathn' def prime_days(sday, eday) sday = Date.today unless sday eday = Date.today unless eday begin sday = Date.parse(sday) eday = Date.parse(eday) rescue puts %Q!invalid date. Usage: prime_days "2012-01-01" "2020-01-01"! return end s = sday.strftime("%Y%m%d").to_i e = eday.strftime("%Y%m%d").to_i days = [] (s..e).each do |num| if num.prime? begin day = Date.parse(num.to_s) days << day.to_s rescue end end end puts "prime days from #{sday} to #{eday}:" days.each{|day| puts day} end prime_days(ARGV[0], ARGV[1])