代码高尔夫:数字的质因数
What is the shortest way, by character count, to find prime factors in any number?
Example Input: 1806046
Example Output: 2x11x11x17x439
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
C#, 69
x 是输入数字
其中包括:
C#, 69
x is input number
With includes:
必填 J 答案(2 个字符):
Obligatory J answer (2 characters):
ANSI C,79 个字符
ANSI C, 79 characters
Mathematica(15 个字符,包括括号):
示例:
Mathematica (15 chars including brackets):
Example:
Python:77 个字符,包含输入和输出
Python: 77 chars with input and output
Haskell,53 个字符:(包括 3 个换行符)
示例:
Haskell, 53 chars: (including 3 newlines)
Example:
Python(没有 I/O 的 228 个字符,有 I/O 的 340 个字符):
可以压缩到 120 个字符:
注意:这是
if
之前的制表符,而不是四个空格。它作为另一级缩进,只需要一个字符而不是两个字符。Python (228 chars without I/O, 340 with):
Can be compressed to 120 chars:
Note: That's a tab character before the
if
, not four spaces. It works as another level of indentation and only costs one character instead of two.F#
81 chars
效率非常低,但由于目标无疑是编写尽可能短的代码,所以我忽略了这一点。
可读形式(使用
#light
语法):F#
81 chars
It's terribly inefficient, but since the aim is undoubtedly to write the shortest code possible, I've neglected that matter.
Readable form (using
#light
syntax):GNU bc,47 个字符,包括收集输入(需要
print
、else
和read
的 GNU 扩展):如果您确实想要输出中的 x 字符,则它是 64 个字符:
另外,请注意,使用 bc 允许处理任意长度的数字。
GNU bc, 47 chars, including collecting input (need the GNU extensions for
print
,else
andread
):If you really want the x characters in the output, it's 64 chars:
Also, note that using bc allows this to process numbers of arbitrary length.
APL 中的 11 个字符
不包括函数头和换行符
11 characters in APL
Excluding function header and newlines
Erlang,核心为 122 个字符,整个模块为 152 个:
从控制台调用:
Erlang, the core is 122 chars and 152 for the whole module:
To call from console:
实际产生指定输出的 Mathematica 答案:
55 个字符。假设
n
是输入数字,并且x
没有分配给它的值。A Mathematica answer that actually produces the specified output:
55 characters. Assumes
n
is the input number andx
doesn't have a value assigned to it.Ruby
39B71B(通过 STDIN)Ruby
39B71B (via STDIN)迄今为止最好的 Perl 答案 - 70 个字符,没有额外的模块(除非您计算 5.10 的特殊功能):
不适用于 1 或 0,但适用于其他一切。如果您不喜欢使用
say
,或者使用的是早期版本的 Perl,这里有一个 81 个字符的版本:Best Perl answer yet - 70 characters, and no extra modules (unless you count special features of 5.10):
Doesn't work for 1 or 0, but works fine for everything else. If you don't like using
say
, or are using an earlier version of Perl, here's an 81 character version:哇,你们不太擅长优化。我可以在 Perl 中用 63 个字符来完成,如果你坚持在顶部包含 #!/usr/bin/perl,则可以使用 79 个字符:(
别那样看我。忠诚的程序员很懒 程序员。)
Wow, you guys aren't very good at optimizing. I can do it in Perl in 63 characters, or 79 if you insist on including a #!/usr/bin/perl at the top:
(Don't look at me that way. Committed programmers are lazy programmers.)
虽然这不是我最好的作品,但这是我在 Haskell 中的答案,83 个字符。
我确信还有更多事情可以做,但目前来说已经很好了。
编辑:重新安排一些东西来削减角色,效率较低,但更小。
While it's not my best work, here's me answer in Haskell, 83 characters.
I'm sure there's more that could be done, but for now it's good.
Edit: Rearranged things to shave off a character, less efficient, but smaller.
Perl,223 个字符
Perl, 223 characters
VB6/VBA - 190 个字符
VB6/VBA - 190 chars
Perl,70 个字符
Perl, 70 char
Euphoria:106 个字符
Euphoria: 106 characters
VB6/VBA - 147 个字符
我不允许留下评论,但可以通过不使用
Option Explicit
来稍微缩短之前的答案。利用 VB6/VBA 的一些更危险的功能,您可以使用以下功能。无需声明变量是什么,如果追求最终的简洁性,也无需将函数声明为公共!如果 End If 位于同一行,则不需要 End If。这可以通过以下方式进行测试:
VB6/VBA - 147 chars
I'm not allowed to leave comments , but it is possible to shorten the previous answer somewhat by not having
Option Explicit
. Taking advantage of some of the more dangerous features of VB6/VBA you can use the one below. No need to declare what the variable is and also the function doesn't need to be declared public either if going for ultimate shortness! Also the End If is not needed if it is on the same line.This can be tested by :
Go 编程语言,100 个字符:
我的程序,具有正确的缩进:
The Go programming language, 100 characters:
My program, with the correct indentation:
74Python 中的 75 个字符源自我的质因数分解 TI-BASIC 代码。
既然我正在谈论 TI-Basic...
TI-Basic 中的 77 个字符
7475 Characters in PythonDerived from my TI-BASIC code for prime factorization.
Since I'm talking about TI-Basic...
77 Characters in TI-Basic
C# 和 LINQ,241 个字符:
压缩:
C# and LINQ, 241 Characters:
Compressed:
C#,366 个字符
对于此类事情,C# 并不是最冗长的语言,但它非常紧凑:
编辑:
我看到 Noldorin 在他的 F# 代码中使用了 List.Find 方法,并意识到它会比 foreach 短一点...
编辑:
好吧,如果它不必是一个完整的程序...
C#,181 个字符
压缩:
C#, 366 characters
C# is not the most averbose language for something like this, but this is quite compact:
Edit:
I saw that Noldorin used the List.Find method in his F# code, and realised that it would be a bit shorter than a foreach...
Edit:
Well, if it doesn't have to be a complete program...
C#, 181 characters
Compressed:
与 Paxinum (Mathematica 答案)类似,这里有一个bash:
7 个字符,排除数字。
In a similar vein as Paxinum (Mathematica answer), here's one in bash:
7 characters the excluding number.
Python递归解法
99个字符(含空格)
87 个字符(不含空格)
更新:
完全递归版本
对于除最小输入之外的所有输入,这两个版本都容易出现堆栈溢出。
Python recursive solution
99 characters (including spaces)
87 characters (without spaces)
Update:
A completely recursive version
Both versions are prone to stack overflows for all but the smallest of inputs.
在 PARLANSE 中,这可以解决问题(252 个字符):
我确信有一个更小的 APL 程序可以做到这一点。
In PARLANSE, this would do the trick (252 chars):
I'm sure there's a much smaller APL program to do this.
Javascript,56
(54 个字符)
首先声明
n= 要分解的数字
(包括 2 个字符),然后执行代码。
例子:
Javascript, 56
(54 characters)
first declare
n= the number to be factored
(2 characters included)then execute the code.
example:
Python 3 163
不打印结果。
将其用作函数:
Python 3 163
Without printing the result.
Use it as a function: