Task: Mertens function




Go: package main

import "fmt"

func mertens(to int) ([]int, int, int) {
    if to < 1 {
        to = 1
    }
    merts := make([]int, to+1)
    primes := []int{2}
    var sum, zeros, crosses int
    for i := 1; i <= to; i++ {
        j := i
        cp := 0      
        spf := false 
        for _, p := range primes {
            if p > j {
                break
            }
            if j%p == 0 {
                j /= p
                cp++
            }
            if j%p == 0 {
                spf = true
                break
            }
        }
        if cp == 0 && i > 2 {
            cp = 1
            primes = append(primes, i)
        }
        if !spf {
            if cp%2 == 0 {
                sum++
            } else {
                sum--
            }
        }
        merts[i] = sum
        if sum == 0 {
            zeros++
            if i > 1 && merts[i-1] != 0 {
                crosses++
            }
        }
    }
    return merts, zeros, crosses
}

func main() {
    merts, zeros, crosses := mertens(1000)
    fmt.Println("Mertens sequence - First 199 terms:")
    for i := 0; i < 200; i++ {
        if i == 0 {
            fmt.Print("    ")
            continue
        }
        if i%20 == 0 {
            fmt.Println()
        }
        fmt.Printf("  % d", merts[i])
    }
    fmt.Println("\n\nEquals zero", zeros, "times between 1 and 1000")
    fmt.Println("\nCrosses zero", crosses, "times between 1 and 1000")
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID.  MERTENS.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 M             PIC S99 OCCURS 1000 TIMES.
          03 N             PIC 9(4).
          03 K             PIC 9(4).
          03 V             PIC 9(4).
          03 IS-ZERO       PIC 99 VALUE 0.
          03 CROSS-ZERO    PIC 99 VALUE 0.

       01 OUTPUT-FORMAT.
          03 OUT-ITEM. 
             05 OUT-NUM    PIC -9.
             05 FILLER     PIC X VALUE SPACE.
          03 OUT-LINE      PIC X(30) VALUE SPACES.
          03 OUT-PTR       PIC 99 VALUE 4.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM GENERATE-MERTENS.
           PERFORM WRITE-TABLE.
           PERFORM COUNT-ZEROES.
           STOP RUN.

       GENERATE-MERTENS.
           MOVE 1 TO M(1).
           PERFORM MERTENS-OUTER-LOOP VARYING N FROM 2 BY 1
               UNTIL N IS GREATER THAN 1000.
    
       MERTENS-OUTER-LOOP.
           MOVE 1 TO M(N).
           PERFORM MERTENS-INNER-LOOP VARYING K FROM 2 BY 1
               UNTIL K IS GREATER THAN N.

       MERTENS-INNER-LOOP.
           DIVIDE N BY K GIVING V.
           SUBTRACT M(V) FROM M(N).

       WRITE-TABLE.
           DISPLAY "The first 99 Mertens numbers are: "
           PERFORM WRITE-ITEM VARYING N FROM 1 BY 1
               UNTIL N IS GREATER THAN 99.
  
       WRITE-ITEM.
           MOVE M(N) TO OUT-NUM.
           STRING OUT-ITEM DELIMITED BY SIZE INTO OUT-LINE
               WITH POINTER OUT-PTR.
           IF OUT-PTR IS EQUAL TO 31,
               DISPLAY OUT-LINE,
               MOVE 1 TO OUT-PTR.

       COUNT-ZEROES.
           PERFORM TEST-N-ZERO VARYING N FROM 2 BY 1
               UNTIL N IS GREATER THAN 1000.
           DISPLAY "M(N) is zero " IS-ZERO " times.".
           DISPLAY "M(N) crosses zero " CROSS-ZERO " times.".

       TEST-N-ZERO.
           IF M(N) IS EQUAL TO ZERO,
               ADD 1 TO IS-ZERO,
               SUBTRACT 1 FROM N GIVING K,
               IF M(K) IS NOT EQUAL TO ZERO,
                   ADD 1 TO CROSS-ZERO.