How to resolve the algorithm Anti-primes step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Anti-primes step by step in the BASIC programming language

Table of Contents

Problem Statement

The anti-primes (or highly composite numbers, sequence A002182 in the OEIS) are the natural numbers with more factors than any smaller than itself.

Generate and show here, the first twenty anti-primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Anti-primes step by step in the BASIC programming language

Source code in the basic programming language

Dim Results(20)
Candidate = 1
max_divisors = 0

Print "Los primeros 20 anti-primos son:"
For j = 0 To 19
	Do
		divisors = count_divisors(Candidate)
		If max_divisors < divisors Then
			Results[j] = Candidate
			max_divisors = divisors
			Exit Do
		End If
		Candidate += 1
	Until false
	Print Results[j];" ";
Next j

Function count_divisors(n)
	cont = 1
	For i = 1 To n/2
		If (n % i) = 0 Then cont += 1
	Next i
	count_divisors = cont
End Function


' convertido desde Ada
Declare Function count_divisors(n As Integer) As Integer

Dim As Integer max_divisors, divisors, results(1 To 20), candidate, j
candidate = 1

Function count_divisors(n As Integer) As Integer
    Dim As Integer i, count = 1
    For i = 1 To n/2
        If (n Mod i) = 0 Then count += 1
    Next i
    count_divisors = count
End Function

Print "Los primeros 20 anti-primos son:"
For j = 1 To 20
    Do
        divisors = count_divisors(Candidate)
        If max_divisors < divisors Then
            Results(j) = Candidate
            max_divisors = divisors
            Exit Do
        End If
        Candidate += 1
    Loop
Next j
For j = 1 To 20
    Print Results(j);
Next j
Print
Sleep

Function count_divisors(n As Integer) As Integer 

  Dim i, count As Integer 
  If n < 2 Then Return 1 
  
  count = 2 
  For i = 2 To n / 2 
    If Not (n Mod i) Then Inc count
  Next 
  Return count 

End Function 

Public Sub Main() 

  Dim count, max_divisors, n, d As Integer 
  
  Print "Los primeros 20 anti-primos son:" 
  While (count < 20) 
    Inc n
    d = count_divisors(n) 
    If d > max_divisors Then 
      Print n; " ";
      max_divisors = d 
      Inc count
    End If
  Wend
  
End

10 REM Anti-primes
20 DEFINT A-Z
30 N=1
40 IF S>=20 THEN END ELSE F=1
50 IF N<2 GOTO 80 ELSE FOR I=1 TO N\2
60 IF N MOD I=0 THEN F=F+1
70 NEXT
80 IF F<=M GOTO 120
90 PRINT N,
100 M=F
110 S=S+1
120 N=N+1
130 GOTO 40

10 REM Anti-primes
20 C = -999
30 N = N + 1
40 GOSUB 70
50 IF T = 20 THEN END
60 GOTO 30
70 D = 0
80 FOR F = 1 TO INT(N/2)
90 IF N MOD F = 0 THEN D = D + 1
100 NEXT F
110 IF D > C THEN GOSUB 130
120 RETURN
130 C = D
140 T = T + 1
150 PRINT N
160 RETURN

100 REM ANTI-PRIMES
110 LET N=1,H=0
120 PRINT "THE FIRST 20 ANTI-PRIMES ARE:"
130 FOR A=1 TO 20
140 GOSUB 300
150 LET H=F
160 PRINT N
170 LET N=N+1
180 NEXT A
190 STOP
290 REM SEARCH NEXT ANTI-PRIME
300 GOSUB 400
310 IF F>H RETURN
320 LET N=N+1
330 GOTO 300
390 REM COUNT DIVISORS
400 LET F=1
410 IF N>1 LET F=2
420 LET C=2
430 IF C*C>=N GOTO 470
440 IF (N/C)*C=N LET F=F+2
450 LET C=C+1
460 GOTO 430
470 IF C*C=N F=F+1
480 RETURN


Procedure.i cntDiv(n.i)
  Define.i i, count  
  If n < 2 : ProcedureReturn 1 : EndIf  
  count = 2 : i = 2  
  While i <= n / 2    
    If n % i = 0 : count + 1 : EndIf
    i + 1
  Wend  
  ProcedureReturn count
EndProcedure

; - - - MAIN - - - 
Define.i n = 1, d, maxDiv = 0, count = 0
If OpenConsole("")
  PrintN("The first 20 anti-primes are: ")      
  While count < 20    
    d = cntDiv(n)
    If d > maxDiv
      Print(Str(n) + " ")
      maxDiv = d : count + 1
    EndIf
    n + 1
  Wend  
  PrintN("")
  Input()
EndIf
End 0

MaxAntiPrime = 20
n = 0
MaxDivisors = 0
AntiPrimeCount = 0
PRINT "The first 20 anti-primes are: "
WHILE AntiPrimeCount < MaxAntiPrime
    n = n + 1
    Divisors = DivisorCount(n)
    IF Divisors > MaxDivisors THEN
        PRINT n;
        MaxDivisors = Divisors
        AntiPrimeCount = AntiPrimeCount + 1
    END IF
WEND
END

FUNCTION DivisorCount (v)
    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


' Anti-primes
DECLARE FUNCTION DivisorCount (V%)

MaxAntiPrime% = 20
N% = 0: MaxDivisors% = 0: AntiPrimeCount% = 0
WHILE AntiPrimeCount% < MaxAntiPrime%
  N% = N% + 1
  Divisors% = DivisorCount(N%)
  IF Divisors% > MaxDivisors% THEN
    PRINT STR$(N%);
    MaxDivisors% = Divisors%
    AntiPrimeCount% = AntiPrimeCount% + 1
  END IF
WEND
PRINT
END

FUNCTION DivisorCount (V%)
  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


MaxAntiPrime = 20
n = 0
MaxDivisors = 0
AntiPrimeCount = 0
print "The first 20 anti-primes are: "
while AntiPrimeCount < MaxAntiPrime
    n = n +1
    Divisors = DivisorCount(n)
    if Divisors > MaxDivisors then
        print n; " ";
        MaxDivisors = Divisors
        AntiPrimeCount = AntiPrimeCount +1
    end if
wend
end

function DivisorCount(v)
    total = 1
    n = v
    while n mod 2 = 0
        total = total +1
        n = int(n / 2)
    wend
    p = 3
    while (p * p) <= n
        count = 1
        while n mod p = 0
            count = count +1
            n = int(n / p)
        wend
        p = p +2
        total = total * count
    wend
    if n > 1 then total = total *2
    DivisorCount = total
end function

100 REM Anti-primes
110 LET A=0
120 LET N=1
130 LET H=0
140 PRINT "The first 20 anti-primes are:"
150 GOSUB 300
160 LET H=F
170 LET A=A+1
180 PRINT N
190 LET N=N+1
200 IF A<20 THEN GOTO 150
210 END
290 REM Search next anti-prime
300 GOSUB 400
310 IF F>H THEN RETURN
320 LET N=N+1
330 GOTO 300
390 REM Count divisors
400 LET F=1
410 IF N>1 THEN LET F=2
420 LET C=2
430 IF C*C>=N THEN GOTO 470
440 IF (N/C)*C=N THEN LET F=F+2
450 LET C=C+1
460 GOTO 430
470 IF C*C=N THEN LET F=F+1
480 RETURN


Private Function factors(n As Integer) As Collection
    Dim f As New Collection
    For i = 1 To Sqr(n)
        If n Mod i = 0 Then
            f.Add i
            If n / i <> i Then f.Add n / i
        End If
    Next i
    f.Add n
    Set factors = f
End Function
Public Sub anti_primes()
    Dim n As Integer, maxd As Integer
    Dim res As New Collection, lenght As Integer
    Dim lf As Integer
    n = 1: maxd = -1
    Length = 0
    Do While res.count < 20
        lf = factors(n).count
        If lf > maxd Then
            res.Add n
            maxd = lf
        End If
        n = n + IIf(n > 1, 2, 1)
    Loop
    Debug.Print "The first 20 anti-primes are:";
    For Each x In res
        Debug.Print x;
    Next x
End Sub

Module Module1

    Function CountDivisors(n As Integer) As Integer
        If n < 2 Then
            Return 1
        End If
        Dim count = 2 '1 and n
        For i = 2 To n \ 2
            If n Mod i = 0 Then
                count += 1
            End If
        Next
        Return count
    End Function

    Sub Main()
        Dim maxDiv, count As Integer
        Console.WriteLine("The first 20 anti-primes are:")

        Dim n = 1
        While count < 20
            Dim d = CountDivisors(n)

            If d > maxDiv Then
                Console.Write("{0} ", n)
                maxDiv = d
                count += 1
            End If
            n += 1
        End While

        Console.WriteLine()
    End Sub

End Module


print "The first 20 anti-primes are:"

while (count < 20)
    n = n + 1
    d = count_divisors(n)
    if d > max_divisors then
        print n;
        max_divisors = d
        count = count + 1
    end if
wend
print

sub count_divisors(n)
    local count, i
    
    if n < 2 return 1
    
    count = 2
    for i = 2 to n/2
      if not(mod(n,  i)) count = count + 1
    next
    return count
end sub

// First 20 antiprimes.
 
sub count_factors(number)
    local count, attempt
    
    for attempt = 1 to number
        if not mod(number, attempt) count = count + 1
    next
    return count
end sub
 
sub antiprimes$(goal)
    local factors, list$, number, mostFactors, nitems
    
    number = 1
    
    while nitems < goal
        factors = count_factors(number)
        if factors > mostFactors then
            list$ = list$ + ", " + str$(number)
            nitems = nitems + 1
            mostFactors = factors
        endif
        number = number + 1
    wend
    return list$
end sub

print "The first 20 antiprimes:"
print mid$(antiprimes$(20), 3)
print "Done."

  

You may also check:How to resolve the algorithm Day of the week step by step in the COBOL programming language
You may also check:How to resolve the algorithm 24 game step by step in the Perl programming language
You may also check:How to resolve the algorithm Documentation step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the TypeScript programming language