is there a faster method to calculate $1/x$ ($x$ an integer) than this?
I gave this stackexchange a second go. Is there a faster way to calculate $1/x$ than the following:
- Calculate $100/x$ (.or other arbitrary positive power of $10$) with remainder
- Write multiplier in hundredths place.
Calculate remainder$/x$ as multiplier times remainder but shifted over to where decimal point is at where you are in $1/x$
repeat step 3, taking your new decimal expansion as the new multiplier (without the $0$ before the decimal point) and square your previous remainder.
ex. $1/97 =$
- $97$ goes $1$ time remainder $3$.
- $0.01$
- $0.0103$
- $0.0103+0.00000927$
- $0.01030927+0.0000000083505087$ ...
5 steps in, we Already have $13$ correct digits. Via group theory it has to have a length that's a divisor of $96$ $(1,2,3,4,6,8,12,16,24,32,48,$ or $96)$ so we've eliminated $7$ of $12$ possible lengths.
Solution 1:
Newton's method for $\frac{1}{A}$ is to iterate $$ x \; \; \; \mapsto \; \; \; \; 2x-A x^2 = x (2-Ax) $$
parisize = 4000000, primelimit = 500000
? a = 97
%1 = 97
?
? x = 0.01
%4 = 0.01000000000000000000000000000
?
? x = x * ( 2 - a * x )
%5 = 0.01030000000000000000000000000
?
? x = x * ( 2 - a * x )
%6 = 0.01030927000000000000000000000
?
? x = x * ( 2 - a * x )
%7 = 0.01030927835050870000000000000
?
? x = x * ( 2 - a * x )
%8 = 0.01030927835051546391752576876
?
? x = x * ( 2 - a * x )
%9 = 0.01030927835051546391752577320
?
Solution 2:
Depends on how many digits precision you want. If you calculate 100/x with an integer x with straightforward long division, the digits will repeat with a period of (x-1). So if the number of digits you want is n >> x, you just calculate the first x-1 digits, and then you repeat them until you have n digits. This runs in O (n), with a small constant factor once you have got the first x digits, which is easily done in c * (x log x) operations.