# I need F(x,y) = xy (not x*y) e.g. x= 1, y =2 so F(x,y) = 12 [closed]

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.

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

fF[x__Integer] := FromDigits[Join @@ IntegerDigits @ {x}]

fF[1, 2]
(* 12 *)
fF[2, 4, 65]
(* 2465 *)

• 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. Jun 17, 2015 at 22:17
• @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.
– kglr
Jun 17, 2015 at 22:25

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.

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.

• 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.
– Neil
Jun 17, 2015 at 20:02
• @Neil you're right, that's an oversight on my part. Corrected to Floor[...] + 1 Jun 17, 2015 at 22:15

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


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]]]

• Nice! An obvious solution to a programmer but not to a mathematician ^_^ Jun 17, 2015 at 19:43