2
$\begingroup$

I just need F(x,y) that give me

e.g. x= 1, y =2 so F(x,y) = 12

Is that possible. I tried Google but not able to find a solution.

Update x and y are integer and i do not want any build-in function rather some mathematical formula. I could have use string concatenation but that has a performance hit that I cannot effort in the application given i have tight loop over millions of values.

I tried 10x + y but i do not know stride length in advance and some time y will not fit into a stride with length 10.

A binary bitwise solution is also acceptable. Given x and y are a 64bit integers.

$\endgroup$
3
  • 5
    $\begingroup$ And what do you need for x = 11 and y = 222? $\endgroup$
    – Kuba
    Jun 17, 2015 at 7:32
  • 1
    $\begingroup$ Also for x=2.34 and y=0.45. On top of that I don't think this question is related to Mathematica. $\endgroup$
    – LLlAMnYP
    Jun 17, 2015 at 7:45
  • 2
    $\begingroup$ …and $10x+y$ does not suit your needs? $\endgroup$ Jun 17, 2015 at 9:55

4 Answers 4

12
$\begingroup$
fF[x__Integer] := FromDigits[Join @@ IntegerDigits @ {x}]

fF[1, 2]
(* 12 *)
fF[2, 4, 65]
(* 2465 *)
$\endgroup$
2
  • $\begingroup$ That's certainly the way to go in Mathematica, although I remain curious, as to what exactly OP desired, as he mentioned not wanting built-in functions. $\endgroup$
    – LLlAMnYP
    Jun 17, 2015 at 22:17
  • $\begingroup$ @LLlAMnYP, i just saw the OP's update re "not wanting a built-in function". Sounds like he needs something along the lines of your answer. $\endgroup$
    – kglr
    Jun 17, 2015 at 22:25
4
$\begingroup$

In light of the update to OP, here's an approach by way of a mathematical formula.

(* f[x_Integer, y_Integer] := x*Power[10, Ceiling[Log[10, y]]] + y *)

update corrected function should be as follows:

f[x_Integer, y_Integer] := x*Power[10, Floor[Log[10, y]] + 1] + y

Now let's make some fake data (two sets of 10^5 64 bit integers):

xlist = RandomInteger[{-2^63, 2^63 - 1}, 10^5];
ylist = RandomInteger[{-2^63, 2^63 - 1}, 10^5];

Let's test the performance.

Thread[f[xlist, ylist]] // AbsoluteTiming // First    

6.7900095

What about strings, as suggested by Peltio?

f2[x_Integer, y_Integer] := ToExpression[StringJoin[ToString[x], ToString[y]]]
Thread[f2[xlist, ylist]] // AbsoluteTiming // First

1.3400019

Better, but not by orders of magnitude. Will not save you if, as you say, you're working with millions of integers.

How about @kguler 's approach?

f3[x_Integer, y_Integer] := FromDigits[Join @@ IntegerDigits@{x, y}]
Thread[f3[xlist, ylist]] // AbsoluteTiming // First

1.0800015

Marginally better.

As we can see, the somewhat universal mathematical formula performs worst of all.

I have a hunch that packed arrays may improve performance, but it appears, that a 64-bit signed integer refuses to fit into a packed array. Also, the result would be at least of 128-bit integers. I'll think about it and update this answer if I come up with something.

$\endgroup$
2
  • $\begingroup$ I don't think Ceiling[Log[10, y]] will work when y is a power of 10, for instance if x = y = 1 then your formula returns 2 instead of 11; try Ceiling[Log[10, y + 1]] or Floor[Log[10, y]] + 1 instead. $\endgroup$
    – Neil
    Jun 17, 2015 at 20:02
  • $\begingroup$ @Neil you're right, that's an oversight on my part. Corrected to Floor[...] + 1 $\endgroup$
    – LLlAMnYP
    Jun 17, 2015 at 22:15
3
$\begingroup$

Yet another way:

f[x_Integer, y_Integer] := x*10^IntegerLength[y] + y

f[68, 54]
6854

Or to generalize:

g = Fold[#1*10^IntegerLength[#2] + #2 &, {##}] &;

g[89, 68, 54]
896854
$\endgroup$
2
$\begingroup$

I was trying to do that with logs and exps but then it dawned on me:

f[x_, y_] := ToExpression[StringJoin[ToString[x], ToString[y]]]
$\endgroup$
1
  • $\begingroup$ Nice! An obvious solution to a programmer but not to a mathematician ^_^ $\endgroup$ Jun 17, 2015 at 19:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.