1000000/gcd(1000000,227273) . Also known as lcm(1000000,227273)/227273 . In this case, 1 million.
What you want to do is 0.227273 per share in its simplest form. The number you are looking for is the denominator of this fraction. Since 227273/1000000 is already in its simplest form, everything is ready. But if your entry was 100.075, then 75/1000 is not in its simplest form. The simplest form is 3/40, so the solution for X is 40.
As an optimization, you can simplify the calculation, because you know that the initial denominator has a power of 10, so its only main factors are 2 and 5. Thus, all you need to look for in the numerator is divisibility by 2 and 5, which is easier than the Euclidean algorithm. Of course, if you already have an implementation of gcd and / or lcm, then this is more effort on your part, no less.
Remember, when you get the result, these floating point numbers may not represent decimals exactly. Therefore, if you have a mathematically correct answer, it will not necessarily give you an integer answer when you perform floating point multiplication. The flip side to this is that, of course, the question only applies if there is a finite decimal expression of the number you are interested in.
If you have a number as a factor in the first place, then you need to find the denominator of its simplest form directly, and not by converting it to decimal and truncating. For example, to solve this problem for the number "6 and one third" the answer is 3, not power 10. If the input is "square root of 2", then for X there is no solution.
Well, actually, the smallest integer X with the property you require is 0 , but I assume that you do not mean this; -)
Steve jessop
source share