6
\$\begingroup\$

Write the shortest program, in the language of your choice, that takes as input a year on the Hebrew calendar, and outputs the corresponding Gregorian date for Rosh Hashana, in YYYY-MM-DD format.

~$ rh 5771
2010-09-09
~$ rh 5772
2011-09-29
~$ rh 5773
2012-09-17
  • The input may come from either the command line or from stdin, whichever is more convenient.
  • You may assume that the year will be between 5600 (1839) and 6000 (2239), inclusive.
\$\endgroup\$
1
  • 2
    \$\begingroup\$ @trinithis i beg your pardon, dude. the Hebrew calendar was inherited from the Babylonian system of computing ephemerides. this guy figured that out: en.wikipedia.org/wiki/Otto_Neugebauer it is a very cool piece of precognate almagestian mathematics worthy of study by any geek worth his unix pipe \$\endgroup\$
    – ixtmixilix
    Sep 22, 2011 at 22:10

3 Answers 3

4
\$\begingroup\$

Ruby, 192 181 165 characters

require'date'
s=(-6+235*n=gets.to_i)/19*765433+73848;e=s/u=25920
$><<Date.jd(347641+e+([h=s%u/19440,84>>e%7+h&1,v=e%7*(374454>>n%19&1-h&s%u/9923),v,n-5765].count 1))

Input is given as a single number on STDIN.

Edit 1: Several arithmetic transformations saved quite a few characters.

Edit 2: There was some room for improvement.

\$\endgroup\$
3
\$\begingroup\$

C#, 212

This might seem cheap, but here I go:

using System;using System.Globalization;class P{static void Main(){int y=int.Parse(Console.ReadLine())-5771;var a=new HebrewCalendar().AddYears(new DateTime(2010,9,9),y);Console.Write(a.ToString("yyyy-MM-dd"));}}

with spacing

using System;
using System.Globalization;
class P
{
    static void Main()
    {
        int y = int.Parse(Console.ReadLine()) - 5771;
        var a = new HebrewCalendar().AddYears(new DateTime(2010, 9, 9), y);
        Console.Write(a.ToString("yyyy-MM-dd"));  
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ You could save one character by using 5771 as your reference year instead of 5772. \$\endgroup\$
    – dan04
    Sep 19, 2011 at 13:33
  • \$\begingroup\$ @dan04 Done.... \$\endgroup\$ Sep 19, 2011 at 13:36
  • \$\begingroup\$ You shouldn't count Console.Read(); as a part of the program, it's not really needed. \$\endgroup\$
    – user1158
    Sep 20, 2011 at 14:48
  • \$\begingroup\$ @PeterOlson Here's a version at only 190 bytes: using System;class P{static void Main(){Console.Write(new System.Globalization.HebrewCalendar().AddYears(new DateTime(2010,9,9),int.Parse(Console.ReadLine())-5771).ToString("yyyy-MM-dd"));}}. \$\endgroup\$
    – Toothbrush
    Feb 19, 2014 at 17:00
1
\$\begingroup\$

CPAN 5.10, 109

This is even cheaper than the C# solution, for reasons enumerated below. Code first: (character count as a difference to perl -E'' as usual)

$ perl -E'$_="DateTime::Calendar::Hebrew";/:/;eval"require $_";say$`->from_object(object,$_->new(year,<>,month,7))->ymd'
  • steals Peter Olson's idea of just outsourcing it to the library
  • CPAN's current release of DateTime::Calendar::Hebrew doesn't install out of the box, you need to patch it.
  • needs Perl 5.10. No earlier, no later.

At least it's short.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.