Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating Anti-Primes
#1
Can this be sped up?   The code below calculates the first 45 anti-primes in ~6.3 seconds on my W11 PC. 
Definition of anti-primes: A natural number that has more divisors (factors, not just prime factors) than any number less that it. For example, 6 has 4 factors, including 1 and itself, and this is more than 1,2,3 or 5 which have only the minimum of 2 factors and 4 which has 3 factors.

Code: (Select All)

'Anti-Primes: Calculate first 45 anti-primes
'date of code: 06 JUL 2023
'Space Ghost (modified QBasic 4.5 from Rosetta Code)

'HOUSEKEEPING -----------------------------------
$CONSOLE:ONLY
OPTION _EXPLICIT
CLS

'VARIABLE DECLARATIONS  -------------------------
DIM t AS DOUBLE
DIM tmp AS STRING
DIM AS INTEGER MaxAntiPrime, AntiPrimeCount
DIM AS LONG MaxDivisors, Divisors, n

'MAIN BLOCK  ------------------------------------
t = TIMER(0.001)

MaxAntiPrime = 45
n = 0
MaxDivisors = 0
AntiPrimeCount = 0

PRINT "The first 45 anti-primes are:"
PRINT

WHILE AntiPrimeCount < MaxAntiPrime
    n = n + 1
    Divisors = DivisorCount(n)
    IF Divisors > MaxDivisors THEN
        PRINT n;
        MaxDivisors = Divisors
        AntiPrimeCount = AntiPrimeCount + 1
    END IF
WEND

PRINT: PRINT
tmp = "Execution Time in secs:##.###"
PRINT USING tmp; TIMER(0.001) - t

END

'FUNCTIONS AND SUBROUTINES ----------------------
FUNCTION DivisorCount (v)
    DIM AS LONG total, count, n, p
    total = 1
    n = v
    WHILE n MOD 2 = 0
        total = total + 1
        n = n \ 2
    WEND
    p = 3
    WHILE (p * p) <= n
        count = 1
        WHILE n MOD p = 0
            count = count + 1
            n = n \ p
        WEND
        p = p + 2
        total = total * count
    WEND
    IF n > 1 THEN total = total * 2
    DivisorCount = total
END FUNCTION

'END OF PROGRAM  --------------------------------

CONSOLE OUTPUT
The first 45 anti-primes are: 1  2  4  6  12  24  36  48  60  120  180  240  360  720  840  1260  1680  2520  5040  7560  10080  15120  20160  25200  27720  45360  50400  55440  83160  110880  166320  221760  277200  332640  498960  554400  665280  720720  1081080  1441440  2162160  2882880  3603600  4324320  6486480

Execution Time in secs: 6.334
Reply


Messages In This Thread
Calculating Anti-Primes - by Space_Ghost - 07-06-2023, 06:56 PM
RE: Calculating Anti-Primes - by bplus - 07-06-2023, 07:16 PM
RE: Calculating Anti-Primes - by Space_Ghost - 07-06-2023, 08:00 PM
RE: Calculating Anti-Primes - by mnrvovrfc - 07-06-2023, 07:54 PM
RE: Calculating Anti-Primes - by Space_Ghost - 07-06-2023, 08:11 PM
RE: Calculating Anti-Primes - by bplus - 07-06-2023, 11:38 PM
RE: Calculating Anti-Primes - by Space_Ghost - 07-06-2023, 11:43 PM
RE: Calculating Anti-Primes - by Jack - 07-07-2023, 11:50 AM
RE: Calculating Anti-Primes - by Space_Ghost - 07-07-2023, 04:38 PM
RE: Calculating Anti-Primes - by Stuart - 07-07-2023, 08:15 PM
RE: Calculating Anti-Primes - by Space_Ghost - 07-07-2023, 10:12 PM
RE: Calculating Anti-Primes - by david_uwi - 07-08-2023, 04:10 PM
RE: Calculating Anti-Primes - by Jack - 07-08-2023, 04:39 PM
RE: Calculating Anti-Primes - by Kernelpanic - 08-13-2023, 12:23 AM
RE: Calculating Anti-Primes - by david_uwi - 07-08-2023, 05:45 PM
RE: Calculating Anti-Primes - by Jack - 07-08-2023, 05:59 PM
RE: Calculating Anti-Primes - by Space_Ghost - 07-08-2023, 07:02 PM
RE: Calculating Anti-Primes - by Jack - 07-08-2023, 07:14 PM



Users browsing this thread: 5 Guest(s)