Basic Maths Shortest C++ Cleanest Solution | Easy to Understand |
The Solution basically consists of 3 things \n\n1.  Counting Zeroes \n2.  Getting Suffix\n3.  Getting Prefix\n\nCounting Zeroes and Suffix can be done easily by
The Solution basically consists of 3 things \n\n1.  Counting Zeroes \n2.  Getting Suffix\n3.  Getting Prefix\n\nCounting Zeroes and Suffix can be done easily by extracting only last 10-12 digits of the answer after every multiplication and counting and removing zeroes as soon as we found them.\n\nNow is the time tricky part :- How to get the first 5 digits ??\nHere comes the Logarithmic property : To get the start 5 digits \n1. Take sum of log(base10)(number) of all the numbers from left to right.\n2. Once the sum is calculated extract the decimal part by ```Decimal(Sum) -  long(Sum)```.\n3. Then Add 4 to it since we want first 5 numbers.\n4. After that lets say you get result Res , Take ```floor(10 power Res)``` and that will be the first 5 digits.\n\nCode Snippet for the above tricky Part\n```\n   long double SumLog= 0;\n        for(int i=left;i<=right;i++)\n        SumLog += log10(i);\n        string P =to_string(floor(pow(10,4.0+(SumLog-long(SumLog)))));\n```\n\nNote : ``` long double ``` is used to get more precision avoid the round off errors which comes with float/double. \n\n```\nclass Solution {\npublic:\n\n    string abbreviateProduct(int left, int right) {\n    \n        string Ans = "1",Prefix ="1";\n        int count = 0;\n        for(int i=left;i<=right;i++)\n        {\n            Ans = to_string(i*stol(Ans));\n            while(Ans.back()==\'0\') Ans.pop_back() , count++;\n            if(Ans.size()>12)\n            Ans =Ans.substr(Ans.size()-12); \n        }\n        \n        long double SumLog= 0;\n        for(int i=left;i<=right;i++)\n        SumLog += log10(i);\n        string P =to_string(floor(pow(10,4.0+(SumLog-long(SumLog)))));\n        \n        if(Ans.size()>10)\n        Ans = (P.size()>5?P.substr(0,5):P) +"..." + Ans.substr(Ans.size()-5);\n        \n        return Ans+"e"+to_string(count);\n    }\n};\n```
['Math', 'C']