Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The PRIME NUMBERS
#51
This one is a 30% faster than my former post
Code:
WIDTH , 50: CLS
PRINT "PRIME CALCULATOR BY ANTONI GUAL agual@eic.ictnet.es"
INPUT "Maximum prime(5 to 2,100,000,000): "; mp&
CONST stor = 3400 'enough to calculate the 5,000,000 first primes

DIM pr(stor) AS LONG   'keeps  2 * prime (the increment to next multiple)
DIM pr1(stor) AS LONG  'keeps the current muliple of the prime

'seed the tables
pr(2) = 2 * 3
pr1(2) = 3 * 3

'init vars
ind& = 2
ind2% = 2
sqre& = 5 * 5

'first prime tested
test& = 5
'just a trick to avoid W2000 to lower priority
OPEN "nul" FOR OUTPUT AS #1
t! = TIMER
PRINT 2, 3,
CNT% = 2
DO
  i% = 1
  DO
    i% = i% + 1
    WHILE pr1(i%) < test&: pr1(i%) = pr1(i%) + pr(i%): WEND
    IF pr1(i%) = test& THEN GOTO noprime
  LOOP UNTIL i% = ind2%
  'it's a prime, print it
  ind& = ind& + 1
  PRINT test&,

  'scroll screen if needed (And keep priority high in W2000)
  CNT% = CNT% + 1: IF CNT% = 250 THEN CLS : CNT% = 0: PRINT #1, ".";

  'add to primes table
  IF ind& <= stor THEN : pr(ind&) = test&: IF ind2% = stor THEN EXIT DO

noprime:
  'go for next prime
  test& = test& + 2

  'if beyond square of last divisor, add a new divisor to tests
  IF test& > sqre& THEN
    ind2% = ind2% + 1: sqre& = pr(ind2%) * pr(ind2%)
    pr1(ind2%) = sqre&
    pr(ind2%) = pr(ind2%) + pr(ind2%)
    IF LEN(INKEY$) THEN EXIT DO
  END IF
LOOP UNTIL test& > mp&

''STOP
ERASE pr, pr1
PRINT : PRINT : PRINT "time for "; ind&; "primes: "; TIMER - t!; "seconds"
a$ = INPUT$(1)

I'm now figuring out how to sort my arrays, it should be even faster
Antoni
Reply


Messages In This Thread
The PRIME NUMBERS - by Touf - 05-05-2003, 05:46 AM
The PRIME NUMBERS - by toonski84 - 05-05-2003, 06:03 AM
The PRIME NUMBERS - by Plasma - 05-05-2003, 06:09 AM
The PRIME NUMBERS - by Touf - 05-05-2003, 05:54 PM
The PRIME NUMBERS - by Touf - 05-07-2003, 08:23 AM
The PRIME NUMBERS - by Mango - 05-07-2003, 11:51 PM
The PRIME NUMBERS - by ak00ma - 05-08-2003, 01:25 AM
The PRIME NUMBERS - by RST - 05-08-2003, 01:34 AM
The PRIME NUMBERS - by DrV - 05-08-2003, 05:52 AM
The PRIME NUMBERS - by Agamemnus - 05-08-2003, 08:04 AM
The PRIME NUMBERS - by RST - 05-08-2003, 07:12 PM
The PRIME NUMBERS - by Touf - 05-08-2003, 10:22 PM
The PRIME NUMBERS - by Touf - 05-12-2003, 05:47 PM
The PRIME NUMBERS - by Mango - 05-12-2003, 07:53 PM
The PRIME NUMBERS - by Touf - 05-13-2003, 02:52 AM
The PRIME NUMBERS - by wizardlife - 05-13-2003, 03:11 AM
The PRIME NUMBERS - by Antoni Gual - 05-14-2003, 02:31 AM
The PRIME NUMBERS - by Touf - 05-29-2003, 05:46 AM
The PRIME NUMBERS - by Mango - 05-29-2003, 10:16 PM
The PRIME NUMBERS - by Touf - 06-02-2003, 04:31 PM
And the Russians beat the French. - by Agamemnus - 06-02-2003, 11:34 PM
The PRIME NUMBERS - by toonski84 - 06-02-2003, 11:39 PM
Re: And the Russians beat the French. - by Mango - 06-03-2003, 01:09 AM
The PRIME NUMBERS - by Antoni Gual - 06-03-2003, 01:44 AM
The PRIME NUMBERS - by Agamemnus - 06-03-2003, 01:49 AM
The PRIME NUMBERS - by whitetiger0990 - 06-03-2003, 01:56 AM
The PRIME NUMBERS - by Antoni Gual - 06-03-2003, 02:47 AM
The PRIME NUMBERS - by Agamemnus - 06-03-2003, 03:02 AM
Toonski - by Mango - 06-03-2003, 04:32 AM
The PRIME NUMBERS - by toonski84 - 06-03-2003, 04:42 AM
The PRIME NUMBERS - by Antoni Gual - 06-03-2003, 04:48 AM
Antonio...re not using squareroots... - by Mango - 06-03-2003, 05:21 AM
The PRIME NUMBERS - by Antoni Gual - 06-03-2003, 05:37 AM
The PRIME NUMBERS - by Touf - 06-03-2003, 02:07 PM
The PRIME NUMBERS - by Touf - 06-03-2003, 05:09 PM
WHAT! WheRE DID MY POST GO? - by Agamemnus - 06-03-2003, 09:15 PM
The PRIME NUMBERS - by Antoni Gual - 06-03-2003, 10:51 PM
The PRIME NUMBERS - by toonski84 - 06-03-2003, 11:04 PM
The PRIME NUMBERS - by Agamemnus - 06-03-2003, 11:10 PM
The PRIME NUMBERS - by toonski84 - 06-03-2003, 11:16 PM
The PRIME NUMBERS - by ak00ma - 06-04-2003, 12:04 AM
The PRIME NUMBERS - by Touf - 06-04-2003, 12:18 AM
The PRIME NUMBERS - by Antoni Gual - 06-04-2003, 01:01 AM
The PRIME NUMBERS - by Agamemnus - 06-04-2003, 01:43 AM
The PRIME NUMBERS - by whitetiger0990 - 06-04-2003, 01:52 AM
The PRIME NUMBERS - by Neo - 06-04-2003, 02:42 PM
The PRIME NUMBERS - by Mango - 06-04-2003, 07:48 PM
The PRIME NUMBERS - by Antoni Gual - 06-05-2003, 04:13 AM
The PRIME NUMBERS - by Agamemnus - 06-05-2003, 04:19 AM
The PRIME NUMBERS - by Neo - 06-05-2003, 12:12 PM
The PRIME NUMBERS - by Antoni Gual - 06-06-2003, 07:01 PM
The PRIME NUMBERS - by Joakim - 06-08-2003, 02:21 AM
The PRIME NUMBERS - by Agamemnus - 06-08-2003, 04:25 AM
The PRIME NUMBERS - by Antoni Gual - 06-08-2003, 06:20 PM
The PRIME NUMBERS - by whitetiger0990 - 06-09-2003, 04:34 AM
The PRIME NUMBERS - by Antoni Gual - 06-09-2003, 05:01 AM
The PRIME NUMBERS - by oracle - 06-09-2003, 05:12 AM
The PRIME NUMBERS - by Antoni Gual - 06-09-2003, 05:45 AM
The PRIME NUMBERS - by oracle - 06-09-2003, 05:46 AM
The PRIME NUMBERS - by Agamemnus - 06-09-2003, 06:59 AM
The PRIME NUMBERS - by whitetiger0990 - 06-09-2003, 07:24 AM
The PRIME NUMBERS - by Agamemnus - 06-09-2003, 07:30 AM
The PRIME NUMBERS - by whitetiger0990 - 06-09-2003, 07:36 AM
The PRIME NUMBERS - by Agamemnus - 06-09-2003, 11:07 PM
The PRIME NUMBERS - by Mango - 06-09-2003, 11:39 PM
The PRIME NUMBERS - by Antoni Gual - 06-09-2003, 11:53 PM
The PRIME NUMBERS - by whitetiger0990 - 06-10-2003, 12:55 AM
The PRIME NUMBERS - by oracle - 06-10-2003, 06:11 AM
The PRIME NUMBERS - by Antoni Gual - 06-11-2003, 03:01 AM
The PRIME NUMBERS - by Mango - 06-13-2003, 12:55 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)