Skip to content

Instantly share code, notes, and snippets.

@ParkinT
Last active August 29, 2015 14:12
Show Gist options
  • Save ParkinT/7eff079f0f72da9d9629 to your computer and use it in GitHub Desktop.
Save ParkinT/7eff079f0f72da9d9629 to your computer and use it in GitHub Desktop.
On the very last day of 2014 I was struck by a thought: Which years - in all of history - are represented by Prime numbers? I intend to expand this with other analyses.
require 'date'
require 'prime'
0.upto(Date.today.year) do | y |
str = y.to_s
upper = str.to_i
prime_year, prime_parts = ''
lower = 2 #non prime in order to fail final 'if'
begin
upper = (str[0..str.size-3]).to_i
lower = (str[-2..-1]).to_i
end if str.size > 2
puts "#{y} is a prime" + ((Prime.prime?(upper) && Prime.prime?(lower)) ? ((str.size > 2) ? " and both #{upper} and #{lower} are prime!" : "" ): "!") if Prime.prime?(y)
end
puts "This year (#{Date.today.year}) is NOT a prime." unless Prime.prime?(Date.today.year)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment