Code Golf:计算东正教复活节日期
挑战
计算希腊东正教复活节的日期 ( http://www.timeanddate.com/holidays/us/orthodox-easter-day)使用最少字符数的给定年份(1900-2100)的星期日。
输入只是“2010”形式的年份。它与获取它的位置(输入、CommandLineArgs 等)无关,但它必须是动态的!
输出应采用日-月-年的形式(例如dd/mm/yyyy
或d/m/yyyy
)
限制 没有标准函数,例如 Mathematica 的 EasterSundayGreekOrthodox
或 PHP 的 easter_date()
,必须使用自动返回(不适用公历)日期!
示例
2005 returns 1/5/2005
2006 returns 23/4/2006
2007 returns 8/4/2007
2008 returns 27/4/2008
2009 returns 19/4/2009
2010 returns 4/4/2010
2011 returns 24/4/2011
2012 returns 15/4/2012
2013 returns 5/5/2013
2014 returns 20/4/2014
2015 returns 12/4/2015
代码计数包括输入/输出(即完整程序)。
编辑: 我指的是东部复活节日期。
The Challenge
Calculate the Date of the Greek Orthodox Easter (http://www.timeanddate.com/holidays/us/orthodox-easter-day) Sunday in a given Year (1900-2100) using the least amount of characters.
Input is just a year in the form '2010'. It's not relevant where you get it (Input, CommandLineArgs etc.) but it must be dynamic!
Output should be in the form day-month-year (say dd/mm/yyyy
or d/m/yyyy
)
Restrictions No standard functions, such as Mathematica's EasterSundayGreekOrthodox
or PHP's easter_date()
, which return the (not applicable gregorian) date automatic must be used!
Examples
2005 returns 1/5/2005
2006 returns 23/4/2006
2007 returns 8/4/2007
2008 returns 27/4/2008
2009 returns 19/4/2009
2010 returns 4/4/2010
2011 returns 24/4/2011
2012 returns 15/4/2012
2013 returns 5/5/2013
2014 returns 20/4/2014
2015 returns 12/4/2015
Code count includes input/output (i.e full program).
Edit:
I mean the Eastern Easter Date.
Reference: http://en.wikipedia.org/wiki/Computus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
Python (
101140132115 个字符)这个使用 Meeus Julian 算法,但由于该算法仅在 1900 年至 2099 年之间有效,因此使用 匿名格里高利算法即将推出。
编辑:现在2005年已经得到妥善处理。感谢马克指出这一点。
编辑 2:几年来更好的处理,感谢所有的投入!
编辑 3:应该适用于范围内的所有年份。 (很抱歉劫持了胡安。)
Python (
101140132115 chars)This one uses the Meeus Julian algorithm but since this one only works between 1900 and 2099, an implementation using Anonymous Gregorian algorithm is coming right up.
Edit: Now 2005 is properly handled. Thanks to Mark for pointing it out.
Edit 2: Better handling of some years, thanks for all the input!
Edit 3: Should work for all years in range. (Sorry for hijacking it Juan.)
PHP CLI,无
easter_date()
,125 个字符适用于 1900 年 3 月 13 日至 2100 年 3 月 13 日的日期,现在适用于 5 月的复活节
代码:
调用:
输出:
使用空格:
由于 PHP 对赋值的处理,此迭代不再可读。它几乎是一种函数式语言!
为了完整起见,这里是之前的 127 个字符的解决方案,不依赖短标签:
代码:
调用:
PHP CLI, no
easter_date()
, 125 charactersValid for dates from 13 March 1900 to 13 March 2100, now works for Easters that fall in May
Code:
Invocation:
Output:
With whitespace:
This iteration is no longer readable thanks to PHP's handling of assignments. It's almost a functional language!
For completeness, here's the previous, 127 character solution that does not rely on short tags:
Code:
Invocation:
C#,155
157182209212个字符class P{static void Main(string[]i ){int y=int.Parse(i[0]),c=(y%19*19+15)%30,d=c+(y%4*2+y%7*4-c+34)% 7+128;System.Console.Write(d%31+d/155+"/"+d/31+"/"+y);}}
Python 2.3, 97 个字符
这也使用 Meeus Julian算法(并且应该适用于五月的日期)。
C#, 155
157182209212charactersclass P{static void Main(string[]i){int y=int.Parse(i[0]),c=(y%19*19+15)%30,d=c+(y%4*2+y%7*4-c+34)%7+128;System.Console.Write(d%31+d/155+"/"+d/31+"/"+y);}}
Python 2.3, 97 characters
This also uses the Meeus Julian algorithm (and should work for dates in May).
Mathematica
调用
输出
我也是: 我不明白不使用 内置函数。
Mathematica
Invoke with
Output
Me too: I don't see the point in not using built-in functions.
Java -
252196190 个字符更新 1: 第一个算法适用于西格里高利复活节。现在固定为东部朱利安复活节。保存了 56 个字符:)
更新 2: 似乎不需要零填充。节省了 4 个字符。
带换行符
Java -
252196190 charsUpdate 1: The first algo was for Western Gregorian Easter. Fixed to Eastern Julian Easter now. Saved 56 chars :)
Update 2: Zero padding seem to not be required. Saved 4 chars.
With newlines
JavaScript(196 个字符)
使用 Meeus Julian 算法。此实现假设给出了有效的四位数年份。
JavaScript (196 characters)
Using the Meeus Julian algorithm. This implementation assumes that a valid four-digit year was given.
Delphi
377335317 个字符单行:
格式:
Delphi
377335317 charactersSingle line:
Formatted:
Tcl
东部复活节
(116 个字符)
使用 Meeus 算法。将年份作为命令行参数,生成Eastern easter。可能是一行行,但拆分时可读性稍高一些...
西方复活节
(拆分行之前有 220 个字符)
使用匿名算法。
Tcl
Eastern Easter
(116 chars)
Uses the Meeus algorithm. Takes the year as a command line argument, produces Eastern easter. Could be a one-liner, but it's slightly more readable when split...
Western Easter
(220 chars before splitting over lines)
Uses the Anonymous algorithm.
COBOL,1262 个字符
注意:不是我的,但我喜欢它
编辑:我添加了带空格的字符计数,但我不知道 COBOL 中的间距如何工作,所以我没有对原始内容进行任何更改。 ~vlad003
更新:我找到了OP从哪里获得此代码:http://www.tek-tips.com/viewthread.cfm?qid=31746&page=112。我把它放在这里只是因为作者应得的。 〜vlad003
COBOL, 1262 chars
Note: Not mine, but I like it
EDIT: I added a char count with spaces but I don't know how spacing works in COBOL so I didn't change anything from original. ~vlad003
UPDATE: I've found where the OP got this code: http://www.tek-tips.com/viewthread.cfm?qid=31746&page=112. I'm just putting this here because the author deserves it. ~vlad003
C,
12812198 个字符回到 Meeus 算法。以儒略历计算日期,但调整公历(这对我来说仍然显得天真,但我找不到更短的替代方案)。
我还没有发现实际需要
floor(d/31)
的情况。此外,为了考虑 5 月份的日期,Meeus 算法中的m
必须至少为 5,因此 DoM 大于 154,因此需要进行除法。年份以程序调用参数的数量加一的形式提供,即。对于 1996 年,您必须提供 1995 年的参数。现代系统上 ARG_MAX 的范围对此绰绰有余。
附言。我发现 Gabe 在 Python 2.3 中也实现了同样的实现,比我高出一个字符。噢。 :(
聚苯硫醚。有人在寻找 1800-2099 年的表格方法吗?
编辑 - 将 Gabe 的答案缩短为 88 个字符:
C,
12812198 charactersBack to Meeus' algorithm. Computing the day in Julian, but adjusting for Gregorian (this still seems naive to me, but I cannot find a shorter alternative).
I have not found a case where
floor(d/31)
would actually be needed. Also, to account for dates in May, them
in Meeus' algorithm must be at least 5, therefore the DoM is greater than 154, hence the division.The year is supplied as the number of program invocation arguments plus one, ie. for 1996 you must provide 1995 arguments. The range of ARG_MAX on modern systems is more than enough for this.
PS. I see Gabe has come to the same implementation in Python 2.3, surpassing me by one character. Aw. :(
PPS. Anybody looking at a tabular method for 1800-2099?
Edit - Shortened Gabe's answer to 88 characters:
BASIC,973 个字符
来源:南澳大利亚天文学会
编辑:我添加字符数,但我认为可以删除许多空格;我不懂 BASIC,所以我没有对代码进行任何更改。 ~vlad003
BASIC, 973 chars
Credit: Astronomical Society of South Australia
EDIT: I added a char count but I think many spaces could be removed; I don't know BASIC so I didn't make any changes to the code. ~vlad003
我不打算实现它,但我希望看到这样一种代码,其中代码向教皇发送电子邮件,扫描返回的任何日期的答案,然后返回该答案。
诚然,调用进程可能会被阻塞一段时间。
I'm not going to implement it, but I'd like to see one where the code e-mails the Pope, scans any answer that comes back for a date, and returns that.
Admittedly, the calling process may be blocked for a while.
Javascript 125 个字符
这将处理 1900 - 2199 年。其他一些实现无法正确处理
2100
年份。Ungolfed..ish
修复了 2399 之前的日期。
我确信有一种方法可以通过算法计算除此之外的日期,但我不想弄清楚。
Javascript 125 characters
This will handle years 1900 - 2199. Some of the other implementations cannot handle the year
2100
correctly.Ungolfed..ish
A fix for dates up to 2399.
I'm sure there is a way to algorithmically calculate dates beyond this but I don't want to figure it out.