在 Mathematica 7 中连接两个整数
在 Mathematica 7 中连接两个正整数最有效的方法是什么?
cc[123, 4567]
>>> 1234567
如果超过两个呢?
cc[123, 4, 567, 89]
>>> 123456789
What is the most efficient way to concatenate two positive integers in Mathematica 7?
cc[123, 4567]
>> 1234567
What about more than two?
cc[123, 4, 567, 89]
>> 123456789
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于许多整数来说,这比上一个解决方案要快一些:
更简洁的替代方案是接受单个参数,假设它是要连接的数字的列表,而不是序列:
它基于
IntegerString
是可列出的
。This will be slightly faster for many integers, than your last solution:
A more concise alternative is to accept a single argument, assuming it to be a list, rather than a sequence, of numbers to concatenate:
which is based on
IntegerString
beingListable
.这仅适用于短整数,因为输出必须是机器大小,但它是我发现的最快的:
对于较长的整数,我能找到的最快的是:
对于连接许多整数,以下比
Fold:
This only works properly for short integers because the output must be machine size, but it is the fastest I found:
For longer integers, the fastest I could find is:
For concatenating many integers, the following was faster than
Fold
on the one above: