Number theory

Table of contents

  1. Zeller's congruence
  2. Pythagorean triples

Zeller's congruence

begin
   public day_of_the_week

   w = ["Satur", "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri"]

   function day_of_the_week(year,month,day)
      J = year//100
      K = (year  if month in 3..12 else year-1)%100
      m = (month if month in 3..12 else month+12)
      n = (day + (m+1)*13//5 + K + K//4 + J//4 - 2*J)%7
      return w[n]+"day"
   end
end

Pythagorean triples

triples = |n| list([x,y,z]
   for x in 1..n
   for y in x..n
   for z in y..n
   if x^2+y^2==z^2)

triples(20).each(print)
# [3, 4, 5]
# [5, 12, 13]
# [6, 8, 10]
# [8, 15, 17]
# [9, 12, 15]
# [12, 16, 20]