Task: Anti-primes








VB: 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


COBOL:       ******************************************************************
      * COBOL solution to Anti-primes challange
      * The program was run on OpenCobolIDE
      ******************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID. ANGLE-PRIMES.

       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       77  ANTI-PRIMES-CTR              PIC 9(3) VALUE 0.
       77  FACTORS-CTR                  PIC 9(3) VALUE 0.
       77  WS-INTEGER                   PIC 9(5) VALUE 1.
       77  WS-MAX                       PIC 9(5) VALUE 0.
       77  WS-I                         PIc 9(5) VALUE 0.
       77  WS-LIMIT                     PIC 9(5) VALUE 1.
       77  WS-REMAINDER                 PIC 9(5).

       01  OUT-HDR         PIC X(23)    VALUE 'SEQ ANTI-PRIME FACTORS'.
       01  OUT-LINE.
           05 OUT-SEQ      PIC 9(3).
           05 FILLER       PIC X(3)     VALUE SPACES.
           05 OUT-ANTI     PIC ZZZZ9.
           05 FILLER       PIC X(4)     VALUE SPACES.
           05 OUT-FACTORS  PIC ZZZZ9.

       PROCEDURE DIVISION.
       000-MAIN.
           DISPLAY OUT-HDR.
           PERFORM 100-GET-ANTI-PRIMES
               VARYING WS-INTEGER FROM 1 By 1
               UNTIL ANTI-PRIMES-CTR >= 20.
           STOP RUN.

       100-GET-ANTI-PRIMES.
           SET FACTORS-CTR TO 0.
           COMPUTE WS-LIMIT = 1 + WS-INTEGER ** .5.
           PERFORM 200-COUNT-FACTORS
               VARYING WS-I FROM 1 BY 1
               UNTIL WS-I >= WS-LIMIT.
           IF FACTORS-CTR > WS-MAX
               ADD 1 TO ANTI-PRIMES-CTR
               COMPUTE WS-MAX = FACTORS-CTR
               MOVE ANTI-PRIMES-CTR TO OUT-SEQ
               MOVE WS-INTEGER TO OUT-ANTI
               MOVE FACTORS-CTR TO OUT-FACTORS
               DISPLAY OUT-LINE
           END-IF.

       200-COUNT-FACTORS.
           COMPUTE WS-REMAINDER =
               FUNCTION MOD(WS-INTEGER WS-I).
           IF WS-REMAINDER = ZERO
               ADD 1 TO FACTORS-CTR
               IF WS-INTEGER NOT = WS-I ** 2
                   ADD 1 TO FACTORS-CTR
               END-IF
           END-IF.

      ******************************************************************
      *    OUTPUT:
      ******************************************************************
      *     SEQ ANTI-PRIME FACTORS
      *     001       1        1
      *     002       2        2
      *     003       4        3
      *     004       6        4
      *     005      12        6
      *     006      24        8
      *     007      36        9
      *     008      48       10
      *     009      60       12
      *     010     120       16
      *     011     180       18
      *     012     240       20
      *     013     360       24
      *     014     720       30
      *     015     840       32
      *     016    1260       36
      *     017    1680       40
      *     018    2520       48
      *     019    5040       60
      *     020    7560       64
      ******************************************************************