TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Identifying Leap Years (2020)

28 点作者 g0xA52A2A11 个月前

4 条评论

squirrel11 个月前
Fun maths, especially computing the inverse of 25 in Z&#x2F;65536Z!<p>As the article mentions, you need a slightly different calculation in the period between 45 BC and 1582 AD -- the rule about 400 didn&#x27;t exist in the Julian calendar in force then. Before 45 BC, and in non-Roman-based calendars, it gets even more complicated with intercalary months and postponing New Year&#x27;s and such.<p>But surely in all but the tiniest fraction of cases, you only care about years after 1582, and years that are no more than a few centuries in the future. There are only 223 leap years from 1582-2500 (I checked with ChatGPT so that number must be right!) A binary search of an ordered 223-item list would require at most 8 comparisons, and if you don&#x27;t mind a little more space, you could just store all 919 of those years in a list and look up the answer directly. Wouldn&#x27;t either be faster than any of these methods (and clearer)?
评论 #40773065 未加载
tppiotrowski11 个月前
Does a compiler use special cases for division by two (and other integers as well) that it implements as single bitwise operations? This seems like a lot of work to scan through all mathematical operations. Is that why compiling code takes so long sometimes?
评论 #40768197 未加载
omoikane11 个月前
I tried implementing the four versions:<p>16bit: <a href="https:&#x2F;&#x2F;gcc.godbolt.org&#x2F;z&#x2F;6G38YxcGr" rel="nofollow">https:&#x2F;&#x2F;gcc.godbolt.org&#x2F;z&#x2F;6G38YxcGr</a><p>32bit: <a href="https:&#x2F;&#x2F;gcc.godbolt.org&#x2F;z&#x2F;vEzf9h1jo" rel="nofollow">https:&#x2F;&#x2F;gcc.godbolt.org&#x2F;z&#x2F;vEzf9h1jo</a><p>I thought it&#x27;s interesting how &quot;div&quot; is used in 16bit versions, whereas for 32bit versions the compiler optimized all of them to use only imul.
bradfitz11 个月前
We need more languages with &quot;moreover&quot; as a keyword.