Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Created April 18, 2017 12:37
Show Gist options
  • Save LuxXx/89e44bd1bfcbc7a0b8d12d2c2670d0ec to your computer and use it in GitHub Desktop.
Save LuxXx/89e44bd1bfcbc7a0b8d12d2c2670d0ec to your computer and use it in GitHub Desktop.
Concatenation of ints using math only
public class Test {
public static void main(String[] args) {
System.out.println(concat(12, 34));
}
public static int concat(int a, int b) {
if (b == 0) return a*10;
return a*pow(10,len(b)) + b;
}
public static int pow (int a, int b) {
if (b == 0) return 1;
int p = 1;
for (int i = 0; i < b; i++) {
p *= a;
}
return p;
}
public static int len(int n) {
if (n == 0) return 0;
if (n < 0) n = -n;
int i = 0;
while (n > 0) {
i++;
n /= 10;
}
return i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment