Concatenate two integers in Mathematica 7

坚强是说给别人听的谎言 提交于 2019-12-05 03:30:58

This will be slightly faster for many integers, than your last solution:

ToExpression[StringJoin @@ Map[IntegerString, {##}]] &

A more concise alternative is to accept a single argument, assuming it to be a list, rather than a sequence, of numbers to concatenate:

ToExpression@StringJoin@IntegerString@#&

which is based on IntegerString being Listable.

This only works properly for short integers because the output must be machine size, but it is the fastest I found:

Compile[{{a, _Integer}, {b, _Integer}}, 
  b + a 10^Floor[1 + Log[10, b]]
]

For longer integers, the fastest I could find is:

FromDigits[{##}, 10^IntegerLength@#2] &

For concatenating many integers, the following was faster than Fold on the one above:

FromDigits[Join @@ IntegerDigits[{##}]] & 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!