3

I gave this stackexchange a second go. Is there a faster way to calculate $1/x$ than the following:

  1. Calculate $100/x$ (.or other arbitrary positive power of $10$) with remainder
  2. Write multiplier in hundredths place.
  3. Calculate remainder$/x$ as multiplier times remainder but shifted over to where decimal point is at where you are in $1/x$

  4. 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 =$

  1. $97$ goes $1$ time remainder $3$.
  2. $0.01$
  3. $0.0103$
  4. $0.0103+0.00000927$
  5. $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.

J. W. Tanner
  • 64,851
  • 5
  • 45
  • 89
  • 1
    Are you trying to calculate $1/x$ as your text says or the number of digits in the repeat of $1/x$ as the final line implies? – Ross Millikan Feb 17 '19 at 14:58
  • 1/x but with knowledge you can calculate reptend length to know when you can stop. –  Feb 17 '19 at 15:02
  • 1
    You know when to stop when the remainder is $1$ because you are back where yo started. I don't have an easier way than long division to get $1/x$ – Ross Millikan Feb 17 '19 at 15:05
  • this method only uses 1 division the rest is squaring, multiplying, shifting and adding. It's based on squaring a number squares it's remainder. –  Feb 17 '19 at 15:07
  • Also where you hit 1 remainder will depend on how you do the division by an arbitrary power of 10. That's partly why the group theory becomes useful. –  Feb 17 '19 at 15:36
  • In this context, "fast" is undefined. Because you don't specify the cost of the operations, e.g. "$97$ goes $1$ time remainder $3$". This is not for free. –  Feb 17 '19 at 15:47
  • I know computers are slow at dividing compared with multiplication and addition. –  Feb 17 '19 at 15:49
  • 2
    It is still unclear what you're asking. You are asking about some class of problems, illustrated by division by $97$. If you intend computation strictly with integers, starting with $x$ an integer, then likely you have in mind a precise form of the output required as the integer form(?) of the repeating part of the rational decimal expansion. However there can be a "leading part" the decimal expansion not included in the repeating part. – hardmath Feb 17 '19 at 19:25

2 Answers2

5

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
? 
Will Jagy
  • 147,694
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.

gnasher729
  • 10,722