12-16-2015, 05:02 AM
Post: #176
 Francois Lanciault Member Posts: 104 Joined: Dec 2013
(12-15-2015 07:23 PM)Francois Lanciault Wrote:  The program computes the quantity of primes numbers less than a given number. So it is mostly integer, with a few square-root and modulo division. It does not used any advanced functions (like ISPRIME or such). It is not the fastest algorithm to do that job but it is faster than checking the primality of every numbers. I have implemented the program on about 100 calculators/computers so I have a good database of results!

I will try to post the code tonight.

François

Hi Claudio,

Here is the code :

For HP-50G

The main program

newma
Code:
 %%HP: T(3)A(R)F(.); \<< TIME SWAP DUP 1. - 2. / IP DUP 1. - 3. / IP - SWAP 0. 0. \-> N J C   \<< 5. N \v/     FOR I       IF I ISPA       THEN 1. - I 2. * 'C' STO C I I * + 'J' STO         WHILE J N \<=         REPEAT 3. I 2. -           FOR K             IF J K MOD 0. ==             THEN 1. SF I 'K' STO             END 2.           STEP           IF 1. FC?C           THEN 1. -           END C 'J' STO+         END       END 2.     STEP   \>> 1. + SWAP TIME SWAP HMS- 100. * \>>

This program uses a subroutine: ISPA as follow

Code:
 %%HP: T(3)A(R)F(.); \<< DUP \v/ IP \-> N R   \<< 1.     DO 2. + DUP DUP     UNTIL R > SWAP N SWAP MOD NOT OR     END R >   \>> \>>

This is the version for the standard HP-50G. For newRPL, I made the following changes:

- removed the dot after each constant (the dot was there because HP-50G is faster in float than in exact arithmetic. I believe newRPL is faster without trailing dot)
- changed "TIME" for "TICKS"
- changed the "C 'J' STO+" sequence to "C J + 'J' STO"

And the corresponding HP-Prime program:

Code:
 ISPR(); EXPORT NEWM(NOM) BEGIN LOCAL II,J,U,K,COMP,TT; LOCAL INC,NBP,RAC; TT:=TICKS; RAC:=IP(√NOM); NBP:=IP((NOM-1)/2); NBP:=NBP-IP((NBP-1)/3); FOR II FROM 5 TO RAC STEP 2 DO IF ISPR(II) THEN NBP:=NBP-1; INC:=2*II; FOR J FROM II²+INC TO NOM STEP INC DO COMP:=0; FOR K FROM 3 TO II-2 STEP 2 DO IF J MOD K == 0 THEN COMP:=1; BREAK; END; END; IF COMP==0 THEN NBP:=NBP-1; END; END; END; END; NBP+1▶NBP; TT:=(TICKS-TT)/1000; PRINT(NBP+" PRIMES IN "+TT+" SEC."); END; ISPR(V) BEGIN LOCAL I; LOCAL RAC; RAC:=√V; FOR I FROM 3 TO RAC STEP 2 DO IF V MOD I==0 THEN RETURN 0; END; END; RETURN 1; END;

Have fun.

Francois
 « Next Oldest | Next Newest »