WeixiangYan/CodeTransOcean huggingface dataset:
Task: Decision tables

C: #include <stdio.h>
 
#define N_COND 3
#define COND_LEN (1 << N_COND)

struct { const char *str, *truth;}
cond[N_COND] = {
	{"Printer does not print",		"1111...."},
	{"A red light is flashing",		"11..11.."},
	{"Printer is unrecognised",		"1.1.1.1."},
},
solu[] = {
	{"Check the power cable",		"..1....."},
	{"Check the printer-computer cable",	"1.1....."},
	{"Ensure printer software is installed","1.1.1.1."},
	{"Check/replace ink",			"11..11.."},
	{"Check for paper jam",			".1.1...."},
};

int main()
{
	int q, ans, c;

	for (q = ans = c = 0; q < N_COND; q++) {
		do {
			if (c != '\n') printf("%s? ", cond[q].str);
			c = getchar();
		} while (c != 'y' && c != 'n');
		ans = (ans << 1) | (c != 'y');
	}

	if (ans == COND_LEN - 1)
		printf("\nSo, you don't have a problem then?\n");
	else {
		printf("\nSolutions:\n");
		for (q = 0; q < sizeof(solu)/sizeof(solu[0]); q++)
			if (solu[q].truth[ans] == '1')
				printf("    %s\n", solu[q].str);
	}
	return 0;
}









COBOL:         >> SOURCE FORMAT FREE
identification division.
program-id. 'decisiontable'.

environment division.
configuration section.
repository.
    function all intrinsic.

data division.

working-storage section.

01  conditions.
    03  notprinting pic x.
    03  flashing pic x.
    03  notrecognized pic x.

procedure division.
start-decision-table.

display space 

display 'The printer does not print (Y or N) ' with no advancing
accept notprinting

display 'A red light is flashing (Y or N) ' with no advancing
accept flashing

display 'The printer is unrecognized (Y or N) ' with no advancing
accept notrecognized

move upper-case(conditions) to conditions

display space
















evaluate notprinting also flashing also notrecognized

when 'Y' also 'Y' also 'Y'
    display 'Check the printer-computer cable'
    display 'Ensure printer software is installed'
    display 'Check/replace ink'

when 'Y' also 'Y' also 'N'
    display 'Check/replace ink'
    display 'Check for paper jam'

when 'Y' also 'N' also 'Y'
    display 'Check the power cable'
    display 'Check the printer-computer cable'
    display 'Ensure printer software is installed'

when 'Y' also 'N' also 'N'
    display 'Check for paper jam'

when 'N' also 'Y' also 'Y'
    display 'Ensure printer software is installed'
    display 'Check/replace ink'

when 'N' also 'Y' also 'N'
    display 'Check/replace ink'

when 'N' also 'N' also 'Y'
    display 'Ensure printer software is installed'

when 'N' also 'N' also 'N'
    display 'no action found'

when other
    display 'invalid input: ' notprinting space flashing space notrecognized

end-evaluate

display space

stop run
.

end program 'decisiontable'.
Task: Numbers divisible by their individual digits, but not by the product of their digits.

C: #include <stdio.h>

int divisible(int n) {
    int p = 1;
    int c, d;
    
    for (c=n; c; c /= 10) {
        d = c % 10;
        if (!d || n % d) return 0;
        p *= d;
    }
    
    return n % p;
}

int main() {
    int n, c=0;
    
    for (n=1; n<1000; n++) {
        if (divisible(n)) {
            printf("%5d", n);
            if (!(++c % 10)) printf("\n");
        }
    }
    printf("\n");
    
    return 0;
}









COBOL:         IDENTIFICATION DIVISION.
        PROGRAM-ID. DIV-BY-DGTS-BUT-NOT-PROD.
        
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 CALCULATION.
           02 N             PIC 9(4).
           02 DGT-PROD      PIC 9(3).
           02 NSTART        PIC 9.
           02 D             PIC 9.
           02 N-INDEXING    REDEFINES N.
              03 DIGIT      PIC 9 OCCURS 4 TIMES.
           02 NDIV          PIC 9(4).
           02 OK            PIC 9.
        01 OUTPUT-FORMAT.
           02 DISP-N        PIC Z(4).
           
        PROCEDURE DIVISION.
        BEGIN.
            PERFORM CHECK VARYING N FROM 1 BY 1 
                    UNTIL N IS EQUAL TO 1000.
            STOP RUN.
            
        CHECK SECTION.
        BEGIN.
            SET NSTART TO 1.
            INSPECT N TALLYING NSTART FOR LEADING '0'.
            
            SET DGT-PROD TO 1.
            PERFORM MUL-DIGIT VARYING D FROM NSTART BY 1
                    UNTIL D IS GREATER THAN 4.
            IF DGT-PROD = 0 GO TO NOPE.
            SET OK TO 1.
            PERFORM CHECK-DIGIT VARYING D FROM NSTART BY 1
                    UNTIL D IS GREATER THAN 4.
            IF OK = 0 GO TO NOPE.
            DIVIDE N BY DGT-PROD GIVING NDIV.
            MULTIPLY DGT-PROD BY NDIV.
            IF NDIV IS EQUAL TO N GO TO NOPE.
            MOVE N TO DISP-N.
            DISPLAY DISP-N.
        MUL-DIGIT.
            IF D IS GREATER THAN 4 GO TO NOPE.
            MULTIPLY DIGIT(D) BY DGT-PROD.
        CHECK-DIGIT.
            IF D IS GREATER THAN 4 GO TO NOPE.
            DIVIDE N BY DIGIT(D) GIVING NDIV.
            MULTIPLY DIGIT(D) BY NDIV.
            IF NDIV IS NOT EQUAL TO N SET OK TO 0.
        NOPE. EXIT.
Task: Day of the week of Christmas and New Year

C: #define _XOPEN_SOURCE
#include <stdio.h>
#include <time.h>

int main() {
    struct tm t[2];
    strptime("2021-12-25", "%F", &t[0]);
    strptime("2022-01-01", "%F", &t[1]);
    for (int i=0; i<2; i++) {
        char buf[32];
        strftime(buf, 32, "%F is a %A", &t[i]);
        puts(buf);
    }
    return 0;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. XMASNY.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WEEKDAYS.
          03 DAY-NAMES.
             05 FILLER   PIC X(9) VALUE "Saturday ".
             05 FILLER   PIC X(9) VALUE "Sunday   ".
             05 FILLER   PIC X(9) VALUE "Monday   ".
             05 FILLER   PIC X(9) VALUE "Tuesday  ".
             05 FILLER   PIC X(9) VALUE "Wednesday".
             05 FILLER   PIC X(9) VALUE "Thursday ".
             05 FILLER   PIC X(9) VALUE "Friday   ".
          03 DAYS        PIC X(9) OCCURS 7 TIMES,
                         REDEFINES DAY-NAMES.
       01 OUT-FMT.
          03 OUT-MONTH   PIC 99.
          03 FILLER      PIC X VALUE "/".
          03 OUT-DAY     PIC 99.
          03 FILLER      PIC X VALUE "/".
          03 OUT-YEAR    PIC 9(4).
          03 FILLER      PIC X(6) VALUE " is a ".
          03 OUT-DAYNAME PIC X(9).
       
       01 VARIABLES.
          03 CDATE.
             05 D-MONTH  PIC 99.
             05 D-DAY    PIC 99.
             05 D-YEAR   PIC 9999.
          03 ZELLER-DATA.
             05 M        PIC 99.
             05 Y        PIC 99.
             05 D        PIC 999.
             05 D7       PIC 999.
             05 J        PIC 99.
             05 K        PIC 99.
             05 DAY-NAME PIC X(9).
       
       PROCEDURE DIVISION.
       BEGIN.
           MOVE "25122021" TO CDATE.
           PERFORM SHOW-WEEKDAY.
           MOVE "01012022" TO CDATE.
           PERFORM SHOW-WEEKDAY.
           STOP RUN.
       
       SHOW-WEEKDAY.
           MOVE D-MONTH TO OUT-MONTH.
           MOVE D-DAY TO OUT-DAY.
           MOVE D-YEAR TO OUT-YEAR.
           PERFORM ZELLER.
           MOVE DAY-NAME TO OUT-DAYNAME.
           DISPLAY OUT-FMT.
       
       ZELLER.
           MOVE D-MONTH TO M.
           MOVE D-YEAR TO Y.
           IF M IS NOT GREATER THAN 2,
               ADD 12 TO M,
               SUBTRACT 1 FROM Y.
           DIVIDE Y BY 100 GIVING J.
           COMPUTE K = Y - J * 100.
           COMPUTE D = (M + 1) * 26.
           DIVIDE 10 INTO D.
           ADD K TO D.
           DIVIDE 4 INTO K.
           ADD K TO D.
           COMPUTE D = D + 5 * J.
           DIVIDE 4 INTO J.
           COMPUTE D = D + J + D-DAY.
           DIVIDE D BY 7 GIVING D7.
           MULTIPLY 7 BY D7.
           COMPUTE D = D - D7 + 1.
           MOVE DAYS(D) TO DAY-NAME.
Task: Loops_Infinite

C: while(1) puts("SPAM");









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Spam.

       PROCEDURE DIVISION.
           PERFORM UNTIL 1 <> 1
               DISPLAY "SPAM"
           END-PERFORM

           GOBACK
           .
Task: Function definition

C: double multiply(double a, double b)
{
   return a * b;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. myTest.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  x   PIC 9(3) VALUE 3.
       01  y   PIC 9(3) VALUE 2.
       01  z   PIC 9(9).
       PROCEDURE DIVISION.
           CALL "myMultiply" USING 
               BY CONTENT x, BY CONTENT y, 
               BY REFERENCE z.
           DISPLAY z.
           STOP RUN.
       END PROGRAM myTest.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. myMultiply.
       DATA DIVISION.
       LINKAGE SECTION.
       01  x   PIC 9(3).
       01  y   PIC 9(3).
       01  z   PIC 9(9).
       PROCEDURE DIVISION USING x, y, z.
           MULTIPLY x BY y GIVING z.
           EXIT PROGRAM.
       END PROGRAM myMultiply.
Task: Terminal control_Dimensions

C: #include <sys/ioctl.h>	
#include <err.h>	
#include <fcntl.h>	
#include <stdio.h>	
#include <unistd.h>	

int
main()
{
	struct winsize ws;
	int fd;

	
	fd = open("/dev/tty", O_RDWR);
	if (fd < 0)
		err(1, "/dev/tty");

	 
	if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
		err(1, "/dev/tty");

	printf("%d rows by %d columns\n", ws.ws_row, ws.ws_col);
	printf("(%d by %d pixels)\n", ws.ws_xpixel, ws.ws_ypixel);

	close(fd);	
	return 0;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. terminal-dimensions.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  num-lines PIC 9(3).
       01  num-cols  PIC 9(3).

       SCREEN SECTION.
       01  display-screen.
           03  LINE 01 COL 01 PIC 9(3) FROM num-lines.
           03  LINE 01 COL 05 VALUE "rows by " .
           03  LINE 01 COL 13 PIC 9(3) FROM num-cols.
           03  LINE 01 COL 16 VALUE " columns.".

       PROCEDURE DIVISION.
           ACCEPT num-lines FROM LINES
           ACCEPT num-cols FROM COLUMNS

           DISPLAY display-screen

      *    This pauses the program, as ncurses will immediately revert
      *    back to the console when the program ends.
           CALL "C$SLEEP" USING BY CONTENT 3

           GOBACK
           .
Task: Sum of the digits of n is substring of n

C: #include <stdio.h>
#include <string.h>

int digitSum(int n) {
    int s = 0;
    do {s += n % 10;} while (n /= 10);
    return s;
}

int digitSumIsSubstring(int n) {
    char s_n[32], s_ds[32];
    sprintf(s_n, "%d", n);
    sprintf(s_ds, "%d", digitSum(n));
    return strstr(s_n, s_ds) != NULL;
}

int main() {
    int i;
    for (i=0; i<1000; i++)
        if (digitSumIsSubstring(i))
            printf("%d ",i);
    printf("\n");
    
    return 0;
}









COBOL:         IDENTIFICATION DIVISION.
        PROGRAM-ID. SUM-SUBSTRING.
            
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 CALCULATION.
           02 N         PIC 9999.
           02 X         PIC 9.
           02 DSUM      PIC 99.
           02 N-DIGITS  REDEFINES N.
              03 ND     PIC 9 OCCURS 4 TIMES.
           02 S-DIGITS  REDEFINES DSUM.
              03 SUMD   PIC 9 OCCURS 2 TIMES.
        01 OUTPUT-FORMAT.
           02 N-OUT     PIC ZZZ9.
        
        PROCEDURE DIVISION.
        BEGIN.
            PERFORM TESTNUMBER VARYING N FROM 0 BY 1
                    UNTIL N IS EQUAL TO 1000.
            STOP RUN.
        
        TESTNUMBER SECTION.
        BEGIN.
            PERFORM SUM-DIGITS.
            SET X TO 1.
            IF DSUM IS LESS THAN 10 GO TO ONE-DIGIT-CHECK.
        
        TWO-DIGIT-CHECK.
            IF X IS GREATER THAN 3 GO TO DONE.
            IF ND(X) = SUMD(1) AND ND(X + 1) = SUMD(2) GO TO SHOW.
            ADD 1 TO X.
            GO TO TWO-DIGIT-CHECK.
            
        ONE-DIGIT-CHECK.
            IF X IS GREATER THAN 4 GO TO DONE.
            IF ND(X) = SUMD(2) GO TO SHOW.
            ADD 1 TO X.
            GO TO ONE-DIGIT-CHECK.
        
        SHOW.
            MOVE N TO N-OUT.
            DISPLAY N-OUT.
        DONE. EXIT.
        
        SUM-DIGITS SECTION.
        BEGIN.
            SET DSUM TO 0.
            SET X TO 1.
        LOOP.    
            ADD ND(X) TO DSUM.
            ADD 1 TO X.
            IF X IS LESS THAN 5 GO TO LOOP.
Task: Create a file on magnetic tape

C: #include<stdio.h>

int main()
{
	FILE* fp = fopen("TAPE.FILE","w");
	
	fprintf(fp,"This code should be able to write a file to magnetic tape.\n");
	fprintf(fp,"The Wikipedia page on Magnetic tape data storage shows that magnetic tapes are still in use.\n");
	fprintf(fp,"In fact, the latest format, at the time of writing this code is TS1155 released in 2017.\n");
	fprintf(fp,"And since C is already 44, maybe 45, years old in 2017, I am sure someone somewhere did use a C compiler on magnetic tapes.\n");
	fprintf(fp,"If you happen to have one, please try to compile and execute me on that system.\n");
	fprintf(fp,"My creator tested me on an i5 machine with SSD and RAM that couldn't have even been dreamt of by Denis Ritchie.\n");
	fprintf(fp,"Who knows ? Maybe he did foresee today, after all he created something which is still young after 44-45 years and counting...\n");
	fprintf(fp,"EOF");
	
	fclose(fp);
	
	return 0;
}









COBOL:        >>SOURCE FORMAT IS FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. MAKE-TAPE-FILE.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT TAPE-FILE
        ASSIGN "./TAPE.FILE"
        ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD TAPE-FILE.
01 TAPE-FILE-RECORD PIC X(51).

PROCEDURE DIVISION.
    OPEN OUTPUT SHARING WITH ALL OTHER TAPE-FILE
    WRITE TAPE-FILE-RECORD 
        FROM "COBOL treats tape files and text files identically."
    END-WRITE
    STOP RUN.
Task: Sorting algorithms_Bead sort

C: #include <stdio.h>
#include <stdlib.h>

void bead_sort(int *a, int len)
{
	int i, j, max, sum;
	unsigned char *beads;
#	define BEAD(i, j) beads[i * max + j]

	for (i = 1, max = a[0]; i < len; i++)
		if (a[i] > max) max = a[i];

	beads = calloc(1, max * len);

	
	for (i = 0; i < len; i++)
		for (j = 0; j < a[i]; j++)
			BEAD(i, j) = 1;

	for (j = 0; j < max; j++) {
		
		for (sum = i = 0; i < len; i++) {
			sum += BEAD(i, j);
			BEAD(i, j) = 0;
		}
		
		for (i = len - sum; i < len; i++) BEAD(i, j) = 1;
	}

	for (i = 0; i < len; i++) {
		for (j = 0; j < max && BEAD(i, j); j++);
		a[i] = j;
	}
	free(beads);
}

int main()
{
	int i, x[] = {5, 3, 1, 7, 4, 1, 1, 20};
	int len = sizeof(x)/sizeof(x[0]);

	bead_sort(x, len);
	for (i = 0; i < len; i++)
		printf("%d\n", x[i]);

	return 0;
}









COBOL:         >>SOURCE FORMAT FREE


identification division.
program-id. beadsort.
environment division.
configuration section.
repository. function all intrinsic.
data division.
working-storage section.
01  filler.
    03  row occurs 9 pic x(9).
    03  r pic 99.
    03  r1 pic 99.
    03  r2 pic 99.
    03  pole pic 99.
    03  a-lim pic 99 value 9.
    03  a pic 99.
    03  array occurs 9 pic 9.
01  NL pic x value x'0A'.
procedure division.
start-beadsort.

    
    compute a = random(seconds-past-midnight)
    perform varying a from 1 by 1 until a > a-lim
        compute array(a) = random() * 10
    end-perform

    perform display-array
    display space 'initial array'

    
    perform varying r from 1 by 1 until r > a-lim
        move all '.' to row(r)
        perform varying pole from 1 by 1 until pole > array(r)
            move 'o' to row(r)(pole:1)
        end-perform
    end-perform
    display NL 'initial beads'
    perform display-beads

    
    perform varying pole from 1 by 1 until pole > a-lim
        move a-lim to r2
        perform find-opening
        compute r1 = r2 - 1
        perform find-bead
        perform until r1 = 0 
            
            move '.' to row(r1)(pole:1)
            move 'o' to row(r2)(pole:1)
            
            compute r2 = r2 - 1
            perform find-opening
            compute r1 = r2 - 1
            perform find-bead
        end-perform
    end-perform
    display NL 'dropped beads'
    perform display-beads

    
    perform varying r from 1 by 1 until r > a-lim
        move 0 to array(r)
        inspect row(r) tallying array(r)
            for all 'o' before initial '.'
    end-perform

    perform display-array
    display space 'sorted array'

    stop run
    .
find-opening.
    perform varying r2 from r2 by -1
    until r2 = 1 or row(r2)(pole:1) = '.'
        continue
    end-perform
    .
find-bead.
    perform varying r1 from r1 by -1
    until r1 = 0 or row(r1)(pole:1) = 'o'
        continue
    end-perform
    .
display-array.
    display space
    perform varying a from 1 by 1 until a > a-lim
        display space array(a) with no advancing
    end-perform
    .
display-beads.
    perform varying r from 1 by 1 until r > a-lim
        display row(r)
    end-perform
    .
end program beadsort.
Task: Execute HQ9+

C: void runCode(const char *code)
{
    int c_len = strlen(code);
    int i, bottles;
    unsigned accumulator=0;
    for(i=0;i<c_len;i++)
    {
        switch(code[i])
        {
            case 'Q':
                printf("%s\n", code);
                break;

            case 'H':
                printf("Hello, world!\n");
                break;

            case '9':
                
                bottles = 99;
                do {
                    printf("%d bottles of beer on the wall\n", bottles);
                    printf("%d bottles of beer\n", bottles);
                    printf("Take one down, pass it around\n");
                    printf("%d bottles of beer on the wall\n\n", --bottles);
                } while( bottles > 0 );
                break;

            case '+':
                
                accumulator++;
                break;
        }
    }
};









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Exec-Hq9.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       78  Code-Length VALUE 256.

       01  i           PIC 999.
       01  accumulator PIC 999.

       01  bottles     PIC 999.

       LINKAGE SECTION.
       01  hq9-code    PIC X(Code-Length).
       
       PROCEDURE DIVISION USING BY VALUE hq9-code.
           PERFORM VARYING i FROM 1 BY 1 UNTIL Code-Length < i
               EVALUATE hq9-code (i:1)
                   WHEN "Q"
                       DISPLAY FUNCTION TRIM(hq9-code)

                   WHEN "H"
                       DISPLAY "Hello, World!"

                   WHEN "9"
                       MOVE 99 TO bottles
                       PERFORM UNTIL bottles = ZERO
                           DISPLAY
                               bottles " bottles of beer on the wall"
                           DISPLAY bottles " bottles of beer"
                           DISPLAY "Take one down, pass it around"
                           SUBTRACT 1 FROM bottles
                           DISPLAY
                               bottles " bottles of beer on the wall"
                           DISPLAY SPACE
                       END-PERFORM

                   WHEN "+"
                       ADD 1 TO accumulator
               END-EVALUATE
           END-PERFORM

           GOBACK
           .
Task: Tau function

C: #include <stdio.h>


unsigned int divisor_count(unsigned int n) {
    unsigned int total = 1;
    
    for (; (n & 1) == 0; n >>= 1) {
        ++total;
    }
    
    for (unsigned int p = 3; p * p <= n; p += 2) {
        unsigned int count = 1;
        for (; n % p == 0; n /= p) {
            ++count;
        }
        total *= count;
    }
    
    if (n > 1) {
        total *= 2;
    }
    return total;
}

int main() {
    const unsigned int limit = 100;
    unsigned int n;

    printf("Count of divisors for the first %d positive integers:\n", limit);
    for (n = 1; n <= limit; ++n) {
        printf("%3d", divisor_count(n));
        if (n % 20 == 0) {
            printf("\n");
        }
    }

    return 0;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. TAU-FUNCTION.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 TAU-VARS.
          03 TOTAL              PIC 999.
          03 N                  PIC 999.
          03 FILLER             REDEFINES N.
             05 FILLER          PIC 99.
             05 FILLER          PIC 9.
                88 N-EVEN       VALUES 0, 2, 4, 6, 8.
          03 P                  PIC 999.
          03 P-SQUARED          PIC 999.
          03 N-DIV-P            PIC 999V999.
          03 FILLER             REDEFINES N-DIV-P.
             05 NEXT-N          PIC 999.
             05 FILLER          PIC 999.
                88 DIVISIBLE    VALUE ZERO.
          03 F-COUNT            PIC 999.
       01 CONTROL-VARS.
          03 I                  PIC 999.
       01 OUT-VARS.
          03 OUT-ITM            PIC ZZ9.
          03 OUT-STR            PIC X(80) VALUE SPACES.
          03 OUT-PTR            PIC 99 VALUE 1.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM SHOW-TAU VARYING I FROM 1 BY 1
               UNTIL I IS GREATER THAN 100.
           STOP RUN.
       
       SHOW-TAU.
           MOVE I TO N.
           PERFORM TAU.
           MOVE TOTAL TO OUT-ITM.
           STRING OUT-ITM DELIMITED BY SIZE INTO OUT-STR
               WITH POINTER OUT-PTR.
           IF OUT-PTR IS EQUAL TO 61,
               DISPLAY OUT-STR,
               MOVE 1 TO OUT-PTR.

       TAU.
           MOVE 1 TO TOTAL.
           PERFORM POWER-OF-2 UNTIL NOT N-EVEN.
           MOVE ZERO TO P-SQUARED.
           PERFORM ODD-FACTOR THRU ODD-FACTOR-LOOP
               VARYING P FROM 3 BY 2
               UNTIL P-SQUARED IS GREATER THAN N.
           IF N IS GREATER THAN 1,
               MULTIPLY 2 BY TOTAL.
       POWER-OF-2.
           ADD 1 TO TOTAL.
           DIVIDE 2 INTO N.
       ODD-FACTOR.
           MULTIPLY P BY P GIVING P-SQUARED.
           MOVE 1 TO F-COUNT.
       ODD-FACTOR-LOOP.
           DIVIDE N BY P GIVING N-DIV-P.
           IF DIVISIBLE,
               MOVE NEXT-N TO N,
               ADD 1 TO F-COUNT,
               GO TO ODD-FACTOR-LOOP.
           MULTIPLY F-COUNT BY TOTAL.
Task: Product of divisors

C: #include <math.h>
#include <stdio.h>


unsigned int divisor_count(unsigned int n) {
    unsigned int total = 1;
    unsigned int p;

    
    for (; (n & 1) == 0; n >>= 1) {
        ++total;
    }
    
    for (p = 3; p * p <= n; p += 2) {
        unsigned int count = 1;
        for (; n % p == 0; n /= p) {
            ++count;
        }
        total *= count;
    }
    
    if (n > 1) {
        total *= 2;
    }
    return total;
}


unsigned int divisor_product(unsigned int n) {
    return pow(n, divisor_count(n) / 2.0);
}

int main() {
    const unsigned int limit = 50;
    unsigned int n;

    printf("Product of divisors for the first %d positive integers:\n", limit);
    for (n = 1; n <= limit; ++n) {
        printf("%11d", divisor_product(n));
        if (n % 5 == 0) {
            printf("\n");
        }
    }

    return 0;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID.  PRODUCT-OF-DIVISORS.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 DIVISOR-PRODUCTS    PIC 9(9) OCCURS 50 TIMES.
          03 NUM                 PIC 999.
          03 MUL                 PIC 999.
      
       01 OUTPUT-FORMAT.
          03 NUM-OUT             PIC Z(9)9.
          03 LINE-PTR            PIC 99 VALUE 1.
          03 OUT-LINE            PIC X(50) VALUE SPACES.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM INIT VARYING NUM FROM 1 BY 1
               UNTIL NUM IS GREATER THAN 50.
           PERFORM CALCULATE-MULTIPLES VARYING MUL FROM 1 BY 1
               UNTIL MUL IS GREATER THAN 50.
           PERFORM OUTPUT-NUM VARYING NUM FROM 1 BY 1
               UNTIL NUM IS GREATER THAN 50.
           STOP RUN.

       INIT.
           MOVE 1 TO DIVISOR-PRODUCTS(NUM).
       
       CALCULATE-MULTIPLES.
           PERFORM MULTIPLY-NUM VARYING NUM FROM MUL BY MUL
               UNTIL NUM IS GREATER THAN 50.
 
       MULTIPLY-NUM.
           MULTIPLY MUL BY DIVISOR-PRODUCTS(NUM).

       OUTPUT-NUM.
           MOVE DIVISOR-PRODUCTS(NUM) TO NUM-OUT.
           STRING NUM-OUT DELIMITED BY SIZE INTO OUT-LINE
               WITH POINTER LINE-PTR.
           IF LINE-PTR IS EQUAL TO 51,
               DISPLAY OUT-LINE,
               MOVE SPACES TO OUT-LINE,
               MOVE 1 TO LINE-PTR.