import
java.util.Random;
class
GFG {
public
static
long
mult(
long
x,
long
y) {
if
(x <
10
&& y <
10
) {
return
x * y;
}
int
noOneLength = numLength(x);
int
noTwoLength = numLength(y);
int
maxNumLength
= Math.max(noOneLength, noTwoLength);
Integer halfMaxNumLength
= (maxNumLength /
2
) + (maxNumLength %
2
);
long
maxNumLengthTen
= (
long
)Math.pow(
10
, halfMaxNumLength);
long
a = x / maxNumLengthTen;
long
b = x % maxNumLengthTen;
long
c = y / maxNumLengthTen;
long
d = y % maxNumLengthTen;
long
z0 = mult(a, c);
long
z1 = mult(a + b, c + d);
long
z2 = mult(b, d);
long
ans = (z0 * (
long
)Math.pow(
10
, halfMaxNumLength *
2
) +
((z1 - z0 - z2) * (
long
)Math.pow(
10
, halfMaxNumLength) + z2));
return
ans;
}
public
static
int
numLength(
long
n)
{
int
noLen =
0
;
while
(n >
0
) {
noLen++;
n /=
10
;
}
return
noLen;
}
public
static
void
main(String[] args)
{
long
expectedProduct =
1234
*
5678
;
long
actualProduct = mult(
1234
,
5678
);
System.out.println(
"Expected 1 : "
+ expectedProduct);
System.out.println(
"Actual 1 : "
+ actualProduct +
"\n\n"
);
assert
(expectedProduct == actualProduct);
expectedProduct =
102
*
313
;
actualProduct = mult(
102
,
313
);
System.out.println(
"Expected 2 : "
+ expectedProduct);
System.out.println(
"Actual 2 : "
+ actualProduct +
"\n\n"
);
assert
(expectedProduct == actualProduct);
expectedProduct =
1345
*
63456
;
actualProduct = mult(
1345
,
63456
);
System.out.println(
"Expected 3 : "
+ expectedProduct);
System.out.println(
"Actual 3 : "
+ actualProduct +
"\n\n"
);
assert
(expectedProduct == actualProduct);
Integer x =
null
;
Integer y =
null
;
Integer MAX_VALUE =
10000
;
Random r =
new
Random();
for
(
int
i =
0
; i < MAX_VALUE; i++) {
x = (
int
) r.nextInt(MAX_VALUE);
y = (
int
) r.nextInt(MAX_VALUE);
expectedProduct = x * y;
if
(i ==
9999
) {
expectedProduct =
1
;
}
actualProduct = mult(x, y);
System.out.println(
"Expected: "
+ expectedProduct);
System.out.println(
"Actual: "
+ actualProduct +
"\n\n"
);
assert
(expectedProduct == actualProduct);
}
}
}