Created
April 18, 2017 12:37
-
-
Save LuxXx/89e44bd1bfcbc7a0b8d12d2c2670d0ec to your computer and use it in GitHub Desktop.
Concatenation of ints using math only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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