素数计算的最短代码
我学校计算机科学系的报纸(名为自述,它是挪威语,第 19 页)进行了一场有趣的竞赛,为以下问题编写尽可能短的 Java 代码。
以一个整数(作为字符串数组的第一个条目中的字符串,因为 Java main 方法只接受字符串数组)作为参数,并首先写出该数字以下的所有素数,然后所有不是素数的数字。 最短的代码获胜!
作为答案,我将发布赢得比赛的最短 Java 代码。 我想知道 Stack Overflow 社区是否可以制作一个更短的代码。如果您懂挪威语,您会发现如果您这样做了,您可能会赢得一瓶香槟,但不幸的是,比赛的最后提交日期已经结束。
你会如何解决这个问题?
The newspaper for the Computer Science-line at my school (called readme, it's norwegian, page 19) had a fun competition to write the shortest possible Java-code for the following problem.
Take in an integer (as a string in the first entry of a string array, since the Java main method only takes a string array) as an argument, and write out first all numbers below this number that are primes, and then all numbers that are not primes. The shortest code wins!
As an answer, I will post the shortest Java-code that won the competition. I wonder if the Stack Overflow Community can make a code that is shorter If you know Norwegian, you will see that you could've won a bottle of champagne if you had done it, but unfortunately the last submit date of the competition is over.
How would you have solved this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在您将标题更改为“Java”之前,我已经在 Haskell 中进行了此操作。 由于这是一个社区 wiki,所以无论如何它都在这里。
(编辑:) 缩短名称并删除空格使其长度为 79 个字符:
这里还交换了结果对的顺序,并根据规范使用了
n-1
。使用次优试除法将其减少到 50 个字符:
I was already doing it in Haskell before you changed the title to "Java". Since this is a community wiki, here it is anyway.
(edit:) Shortening names and removing whitespace makes it 79 chars:
here also the resulting pair's order is swapped, and
n-1
is used, as per the spec.Using sub-optimal trial division brings it down to 50 chars:
赢得比赛的 Java 代码(153 字节,无空格,此处包含空格是为了便于阅读):
The Java-code that won the competition (153 bytes without spacing, spacing included here for readability):
只是为了好玩,这里是之前的 Haskell 答案的 Java 版本。 如果有足够的堆,该程序将针对所有参数终止。
fj 软件包来自此处。
Just for fun, here's a Java version of the previous Haskell answer. This program terminates for all arguments, given sufficient heap.
The fj package is from here.
我在 Ruby 中的尝试。 93 个字符。
My attempt in Ruby. 93 characters.
如果您想要 js 代码:n 是最大值(62 个字符)
if you would like a js code : n is the max (62 chars)
Python,65 个字符
用法:
Python, 65 characters
Usage:
易于理解且智能的代码可能如下所示:
An easy to understand and smart code may be like:
为了完整起见,还有两个 Haskell 定义。 简洁的原型
和绝对冠军,
For completeness, two more Haskell definitions. The archetypal
and the absolute champion in brevity,
133 个字符:-)
133 characters :-)