The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

HP 39gii Tutorial: REPEAT and WHILE
Message #1 Posted by Eddie W. Shore on 18 Mar 2013, 10:52 p.m.

http://edspi31415.blogspot.com/2013/03/hp-39gii-programming-part-4-repeat-and.html?m=1

      
Re: HP 39gii Tutorial: REPEAT and WHILE
Message #2 Posted by Gilles Carpentier on 19 Mar 2013, 7:20 a.m.,
in response to message #1 by Eddie W. Shore

Hi Eddie

here is an improved version of your SQFACTOR program :

EXPORT SQFACTOR(N)
 BEGIN
  LOCAL C,K,S;
  C:=1;
  K:=FLOOR(VN);   // V for square root symbol
  WHILE K > 1 DO
   WHILE FRAC(N/K²) == 0 DO
    N:=N/K²;
    C:=C*K;
   END;
   K:=K-1;
  END;
  S:="";
  IF C<>1 THEN S:=string(C);END;
  IF N<>1 THEN S:=S+"V"+string(N); END;
  RETURN S;
 END;

So SQFACTOR(400) returns "20" and not "4 &#8730; 25"

Edited: 19 Mar 2013, 7:26 a.m.

            
Re: HP 39gii Tutorial: REPEAT and WHILE
Message #3 Posted by Eddie W. Shore on 20 Mar 2013, 11:25 a.m.,
in response to message #2 by Gilles Carpentier

Thanks Giles! I am going to test this out

                  
Re: HP 39gii Tutorial: REPEAT and WHILE
Message #4 Posted by Gilles Carpentier on 20 Mar 2013, 3:27 p.m.,
in response to message #3 by Eddie W. Shore

Here is an interesting program by C.RET posted on silicium.org

EXPORT RAC(X)
BEGIN
 LOCAL F:=SIGN(X), R:=ABS(X);
 LOCAL I:=1,Txt:="V("+X+")=";

REPEAT IF R MOD I*I THEN I:=I-1; ELSE F:=F*I; R:=R/I/I; I:=FLOOR(VR); END; UNTIL I<2;

IF (ABS(F)<>1) OR (R=F) THEN Txt:=Txt+ABS(F) END; IF F<0 THEN Txt:=Txt+" i " END; IF R<>1 THEN Txt:=Txt+"V"+R END; RETURN Txt; END;

Change V by square root symbol it works also with negative numbers :

                        
Re: HP 39gii Tutorial: REPEAT and WHILE
Message #5 Posted by Eddie W. Shore on 20 Mar 2013, 4:10 p.m.,
in response to message #4 by Gilles Carpentier

I did not know we could initialize and localize variables in one step. This will make things more efficient.

Gilles, is it OK if I post your program in my next blog entry? Credit will be given.

Eddie

                              
Re: HP 39gii Tutorial: REPEAT and WHILE
Message #6 Posted by Gilles Carpentier on 20 Mar 2013, 4:26 p.m.,
in response to message #5 by Eddie W. Shore

Hi Eddie, this program is not by me, but by C.RET.

                              
Re: HP 39gii Tutorial: REPEAT and WHILE
Message #7 Posted by C.Ret on 20 Mar 2013, 6:35 p.m.,
in response to message #5 by Eddie W. Shore

Eddie you are welcome.

No problem at all, I consider this code has already fallen into the public domain since I posted it this public forum. It was the result of a kid of brain storm threat. Several people were involved in the discussion. Gilles is one of them.

Please feel free to publish it, as soon as you are kind enough to indicate references of the peoples and the Silicium.org forum.

www.silicium.org/forum [hp39gII] Simplificateur de racines carrées ([hp39gii] A Square Roots Simplicator)

The HP-39gii is really an amazing system. Every day, we discover new capabilities; the localization and initialization in one shot is one of its numerous features.

The Silicium.org forum is really active, we already analyze this first version of the code and turn it into two new versions :

EXPORT RAC(X)
BEGIN
 LOCAL F:=SIGN(X), R:=ABS(X);
 LOCAL I:=1,Txt:="¡Ì("+X+")=";

REPEAT IF R MOD I*I THEN I:=prevprime(I); ELSE F:=F*I; R:=R/I/I; I:=FLOOR(¡ÌR); END; UNTIL Q<2;

IF (ABS(F)<>1) OR (R=F) THEN Txt:=Txt+ABS(F) END; IF F<0 THEN Txt:=Txt+" i " END; IF R<>1 THEN Txt:=Txt+" \V"+R END; RETURN Txt; END;

The only change is prevprime(I) replacing I:=I-1; expecting speed-up with large numbers.

The last version, is quite more different, it is using the built-in function ifactors that returns the list of prime factors (and multiplicity). This last version has the shortest/fastest look-up loop !

EXPORT RAC( X )
BEGIN
  LOCAL F:=SIGN(X),R:=ABS(X);
  LOCAL p,I,T:="¡Ì("+X+")=";

LOCAL LF:=ifactors(R);

FOR p FROM 2 TO SIZE(LF) STEP 2 DO I:=LF(p-1)^FLOOR(LF(p)/2); F:=F*I; R:=R/I/I; END;

IF (ABS(F)<>1) OR (R=F) THEN T:=T+ABS(F) END; IF F<0 THEN T:=T+" i " END; IF R<>1 THEN T:=T+" \V"+R END; RETURN T; END;

Edited: 20 Mar 2013, 6:40 p.m.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall