Post Reply 
Repeating Decimals / Recurring Decimals
01-21-2018, 11:01 AM (This post was last modified: 01-21-2018 11:34 PM by StephenG1CMZ.)
Post: #2
RE: Repeating Decimals
(PPL) Version 0.2 follows the sequence of steps in Joe Horn's algorithm using MultiplicateOrder, which can be found here: http://www.hpmuseum.org/forum/thread-3212.html

Output is currently a list:
1: {Length of transient, transient}
2: {Length of recurring part, both parts but omitting the 0. Or 1.}
3: Just as a check, the real value.
The formatting could be improved.

Note that if the length of the recurring part exceeds 12 the recurring part returned is unreliable and should probably be replaced by NaN (the indicated length is OK, but not the digits...and sometimes they are not digits).

Code:

 LOCAL CRID:="REPEATING DECIMAL (PPL) V 0.2";

 //Written 2018 StephenG1CMZ
 //Following Joe Horn`s algorithm using Multiplicative Order

 //This implement yields some useful results but
 //Large Repeating Lengths (>12repeatingdigits) yield weird output eg NN=301

 //The output format could be improved.

 LOCAL ImpRELEN:=12;//EMPIRICAL

 LOCAL FR2,FR5;
 EXPORT TESTED:={};

 ZEROS(NN)
 BEGIN
  IF NN>0 THEN
   RETURN "0"+ZEROS(NN-1);
  END;
  RETURN ""; 
 END;

 TRUNCRR(NN,DIGITS)
 //TRUNCATE A REAL RECIPROCAL
 //NN:DENOMINATOR
 //DIGITS:NUMBER OF PLACES TO KEEP
 BEGIN
  LOCAL TP;
  TP:=(1/NN)*10^DIGITS;
  TP:=IP(TP)/(10^DIGITS);
  RETURN TP;
 END;
  
 EXPORT MAXFACTOR25(NN)
 BEGIN 
  LOCAL MAXFACTR:=0;
  LOCAL LST,LSTB,LP,II;
  
  LST:=mat2list(ifactors(NN)); //BASES AND COUNTSETC
  //EXTRACT BASES (2,5) ETC
  LSTB:=IFTE(SIZE(LST),MAKELIST(LST(2*II-1),II,1,(SIZE(LST)/2)),{});//{} HANDLES NN=1 
 
  FR2:=0; FR5:=0;
  LP:=POS(LSTB,2); 
  IF LP THEN
   FR2:=LST(2*LP);//EXPONENT
  END;
  LP:=POS(LSTB,5); 
  IF LP THEN
   FR5:=LST(2*LP);//EXPONENT
  END;
  
  MAXFACTR:=MAX(FR2,FR5);
  RETURN MAXFACTR;//0=NONE
 END;

 EXPORT GETTRANSIENT(NN)
 BEGIN
  LOCAL TRANSIENTLEN,TRANSIENTPART;

  TRANSIENTLEN:=MAXFACTOR25(NN);
  TRANSIENTPART:=TRUNCRR(NN,TRANSIENTLEN);
 
  RETURN {TRANSIENTLEN,TRANSIENTPART};
 END;

 EXPORT ReDIGITS (NN )
 //NN INTEGER 
 BEGIN
  LOCAL TRANSIENT; //TRANSIENTLEN,TRANSIENTPART;
  LOCAL WHATSLEFT,MO;
  LOCAL RP,NDIGITS;
  LOCAL ST;//STRINGVERSION
  LOCAL RESULTS:={};
  LOCAL RL;
  IF NN THEN
   RL:=1/NN;//DIAGNOSTIC ONLY:USUAL REAL
  ELSE
   RETURN {"DIVBY0"};
  END;
  TRANSIENT:=GETTRANSIENT(NN); 
  WHATSLEFT:=exact(NN/(2^FR2*5^FR5));

  IF WHATSLEFT==1 THEN
   //AVOID STALL
   RETURN {TRANSIENT,{0,0},RL};//AVOID STALL
  END;
  //PRINT("BEFORE");
  //See http://www.hpmuseum.org/forum/thread-3212.html
  MO:=MultiplicativeOrder(WHATSLEFT,10);
  //PRINT("AFTER");

  NDIGITS:=TRANSIENT(1)+MO;//TRANSIENTLEN
  IF MO>ImpRELEN THEN
   RP:="(NaN)";
  END;//FOR NOW SHOW THE WEIRD OUTPUT
  RP:=iquo(1*10^NDIGITS,NN);
  //The string version asks for leading zeros I hoped
  //but no...pad it manually 
  //ST:=format(RP,"s"+NDIGITS);
   
  ST:=ZEROS(NDIGITS-DIM(STRING(RP)))+RP;

  RESULTS:={TRANSIENT,{MO,ST},RL};//
 
  IF 0 THEN //DEBUG INFO
   PRINT(); 
   PRINT("Input Fraction: 1/"+NN+" = "+RL);
   PRINT("Whats Left: "+WHATSLEFT);
   PRINT("Multiplicative Order: "+MO);
   //PRINT("Length: "+{TRANSIENTLEN,MO});
   IF MO>12 THEN //EXPECT REPEATING PART TO GO WEIRD
    PRINT("MO>12: CAUTION");
   END;
   PRINT("TRANSIENT PART: "+IFTE(TRANSIENT(1),TRANSIENT(2),TRANSIENT(2)+" (none)"));
   PRINT(ST);
   PRINT(RESULTS);
  END;
  
  //RETURN RESULTS
  //TRANSIENTLEN=0 =NO TRANSIENT
  //MO=0 =NO REPEATING
  //MO>Imp: IGNORE REPEATING DIGITS
  //RL:REAL JUST FOR COMPARISON
  RETURN RESULTS;
 END;

 EXPORT TESTS()
 BEGIN
  LOCAL III,RR;
  FOR III FROM 1 TO 1000  DO
   //RECT_P();
   //TEXTOUT_P(III,0,120);
    //WAIT(2);
   TESTED(0):=III;
   RR:=ReDIGITS(III);  
  END;
 END;

 EXPORT REDECIMALS()
 BEGIN
  PRINT(CRID);  
  //RECT_P();
  PRINT(TEVAL(TESTS));
 END;

Note: A comparable CAS implementation could improve ranges handled.

Update: 99989 crashes the Android emulator.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Repeating Decimals - StephenG1CMZ - 01-21-2018 11:01 AM



User(s) browsing this thread: 2 Guest(s)