Primes play an important part in number theory and geometry; mathematicians since the Greeks have investigated their properties. They are also used in cryptography , for generating random numbers and for hashing in databases. The one-line program generates the first 7500 prime numbers. It takes just under half an hour to run, so it seems a shame not to add a second line and save the data, which takes up 50k. 1 ' Prime generator 10 DIM p(7500): p(0)=1: p(1)=2: p(2)=3: FOR n=3 TO 7500: p(n)=p(n-1): ok=0: WHILE ok=0: ok=1: p(n)=p(n)+2: s=SQR(p(n)): m=0: WHILE ok*(p(m+1)<=s): m=m+1: d=p(n)/p(m): ok=d-INT(d): WEND:WEND: PRINT n,p(n):NEXT 20 OPENOUT"primdat": FOR n=1 TO 7500: WRITE #9,p(n): NEXT: CLOSEOUT99 ' Prime finder 100 DIM p(7500): p(0)=1: OPENIN "primdat" 110 FOR n=1 TO 7500: INPUT #9,p(n): NEXT 120 WHILE -1 130 INPUT"Search from what number (0 to quit) ";x 140 IF x=0 THEN END 150 ok=0: x=x-1: IF x160 WHILE ok=0 170 ok=1: m=0: x=x+1: s=SQR(x) 180 WHILE ok*(p(m+1)<=s) 190 m=m+1: d=x/p(m): ok=d-INT(d) 200 WEND: WEND 210 PRINT"THE NEXT PRIME IS: ";x 220 WENDIt should be possible to extend the program to calculate primes up to 10^9. the accuracy limit of the Amstrad. The results would have to be saved to disk, for there is no space to extend the data array. The second program allows you to find a range of prime numbers from among the saved data. AA ★ AMSTRAD CPC ★ A voir aussi sur CPCrulez , les sujets suivants pourront vous intéresser... |
|
|