There are 2 large integers, x and y such that z=x*y overflows. I would like to compute the first digit of x. Directly doing so isn't possible as the result overflows.
I was wondering if there was a certain technique for this.
Example: 10000000000000000000000000000000000 * 20579725928294522859735727575, here the first digit is 2 as I can see it directly, however, besides this special case, is there a way?
#include <iostream>
using namespace std;
int main() {
long long int x,y,z;
cin>>x>>y;
z=x*y;
while(z>9)
{
z/=10;
}
cout<<z;
return 0;
}
Above is a code that fails to work. For x=10000000000000000000000000000000000 y=20579725928294522859735727575
It gives output 5 as z overflows, and thus z=5240626126797720724 which is clearly wrong. I am looking to find a way out of this.