IDENTIFICATION DIVISION.
PROGRAM-ID. 162C.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 K          PIC  9(3).
01 A          PIC  9(3) COMP.
01 B          PIC  9(3) COMP.
01 C          PIC  9(3) COMP.
01 I          PIC  9(3) COMP.
01 I1         PIC  9(3) COMP.
01 I2         PIC  9(3) COMP.
01 I3         PIC  9(3) COMP.
01 I1-M       PIC  9(3) COMP.
01 I2-M       PIC  9(3) COMP.
01 I3-M       PIC  9(3) COMP.
01 SI         PIC  9(2) COMP.
01 G          PIC  9(3) COMP.
01 S          PIC  9(3) COMP.
01 AMR        PIC  9(3) COMP.
01 GK         PIC  9(10) COMP.
01 OUT        PIC  Z(9)9.
01 SOSUU.
   03 F1 PIC X(48) VALUE '002003005007011013017019023029031037041043047053'.
   03 F2 PIC X(48) VALUE '059061067071073079083089097101103107109113127131'.
   03 F3 PIC X(42) VALUE '137139149151157163167173179181191193197199'.
01 SOSUU-TBL REDEFINES SOSUU.
   03 S-T     PIC 9(3) OCCURS 46.
01 A-TBL.
   03 AN      PIC 9(2) OCCURS 200.
01 B-TBL.
   03 BN      PIC 9(2) OCCURS 200.
01 C-TBL.
   03 CN      PIC 9(2) OCCURS 200.

*>
PROCEDURE DIVISION.
  ACCEPT K.
*>
  PERFORM VARYING A FROM 1 BY 1 UNTIL A > K
    PERFORM VARYING B FROM 1 BY 1 UNTIL B > K
      PERFORM VARYING C FROM 1 BY 1 UNTIL C > K
      PERFORM GCD
        ADD G TO GK
      END-PERFORM
    END-PERFORM
  END-PERFORM
*>
  MOVE GK TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
*>---------------------------------------------<*
GCD SECTION.
  MOVE ZERO TO A-TBL B-TBL C-TBL.
*>
  MOVE 1 TO I1 I1-M
  MOVE A TO S
  PERFORM UNTIL S < S-T(I1)
    DIVIDE S BY S-T(I1) GIVING S REMAINDER AMR
    IF AMR = 0
      ADD 1 TO AN(I1)
      MOVE I1 TO I1-M
    ELSE
      ADD 1 TO I1
    END-IF
  END-PERFORM.
*>
  MOVE 1 TO I2 I2-M
  MOVE B TO S
  PERFORM UNTIL S < S-T(I2)
    DIVIDE S BY S-T(I2) GIVING S REMAINDER AMR
    IF AMR = 0
      ADD 1 TO BN(I2)
      MOVE I2 TO I2-M
    ELSE
      ADD 1 TO I2
    END-IF
  END-PERFORM.
*>
  MOVE 1 TO I3 I3-M
  MOVE C TO S
  PERFORM UNTIL S < S-T(I3)
    DIVIDE S BY S-T(I3) GIVING S REMAINDER AMR
    IF AMR = 0
      ADD 1 TO CN(I3)
      MOVE I3 TO I3-M
    ELSE
      ADD 1 TO I3
    END-IF
  END-PERFORM.
*>
  MOVE 0 TO G.
  MOVE 1 TO I
  PERFORM UNTIL I > I1-M OR I > I2-M OR I > I3-M
    IF AN(I) > 0 AND BN(I) > 0 AND CN(I) > 0
      MOVE FUNCTION MIN(AN(I), BN(I), CN(I)) TO SI
      COMPUTE G = G + S-T(I) ** SI
    END-IF
    ADD 1 TO I
  END-PERFORM.
*>
  IF G = 0 
    MOVE 1 TO G
  END-IF.
*>
  EXIT.
