07-06-2023, 06:56 PM
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.
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
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 OUTPUTThe 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