## Nth Prime number

Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

### Nth Prime number

Hi,

Can anyone help me to get the logic to find Nth prime number in cobol. Below is the logic to check if the number is prime or not.But a bit confused while implementing logic to get Nth prime number like if I enter 3 then it should give result as 5 and If i enter 4 then it should give 7 because the prime number series is like- 2,3,5,7,11,13 and so on.

WORKING-STORAGE SECTION.
77 N PIC 9(3).
77 Q PIC 9(3).
77 R PIC 9(3).
77 I PIC 9(3).

PROCEDURE DIVISION.
PARA-A.
DISPLAY ( 1 , 1 ) ERASE.
DISPLAY ( 2 , 1 ) "ENTER AN INTEGER:".
ACCEPT ( 2 , 20 ) N.
PERFORM PARA2 VARYING I FROM 2 BY 1 UNTIL I > N.
STOP RUN.

PARA2.
DIVIDE N BY I GIVING Q REMAINDER R.
IF R==0
DISPLAY(5, 1) "NOT PRIME"
ELSE
DISPLAY(5, 1) "PRIME".
gauravnnl

Posts: 21
Joined: Tue Jul 15, 2014 2:25 pm
Has thanked: 3 times
Been thanked: 0 time

### Re: Nth Prime number

1. Find all prime numbers up to a given value
2. Return the nth prime number.

For what you are wanting, a sieve of Eratosthenes would be a better way to go. Use a table and set the non-prime values to zero. When you have generated your prime values, to find the nth prime number, merely iterate through the table, returning the nth non-zero number.
Robert Sample
Global moderator

Posts: 3652
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 269 times

### Re: Nth Prime number

Here is the code...if anyone want to refer--

IDENTIFICATION DIVISION.

PROGRAM-ID.    PRIME.

AUTHOR.        XYZ.

DATE-WRITTEN.  JULY 2014.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-370 WITH DEBUGGING MODE.

DATA DIVISION.
WORKING-STORAGE SECTION.

01 WS-VAR.
10 WS-INPUT              PIC 9(04) VALUE ZEROES.

10 WS-OUT                PIC 9(04) VALUE ZEROES.

10 WS-TEMP-1             PIC 9(4)  VALUE 0.
10 WS-TEMP-2             PIC 9(4)  VALUE 0.
10 WS-TEMP-3             PIC 9(4)  VALUE 0.
10 WS-TEMP-4             PIC 9(4)  VALUE 0.
10 WS-TEMP-5             PIC 9(4)  VALUE 0.

PROCEDURE DIVISION.
ACCEPT WS-INPUT.
DISPLAY 'INPUT:' WS-INPUT
MOVE 1 TO WS-TEMP-1
MOVE 2 TO WS-OUT
MOVE 2 TO WS-TEMP-2

IF WS-TEMP-1 = WS-INPUT
DISPLAY WS-INPUT 'TH PRIME NO IS:' WS-OUT
GO TO 500-PARA
ELSE
COMPUTE WS-OUT = WS-OUT + 1
PERFORM 100-PARA
END-IF.

100-PARA.
PERFORM 200-PARA UNTIL WS-TEMP-2 >= WS-OUT
PERFORM 300-PARA.

200-PARA.
DIVIDE WS-OUT BY WS-TEMP-2 GIVING    WS-TEMP-3
REMAINDER WS-TEMP-4
IF WS-TEMP-4 = 0
COMPUTE WS-OUT = WS-OUT + 1
MOVE 2 TO WS-TEMP-2
ELSE
COMPUTE WS-TEMP-2 = WS-TEMP-2 + 1
END-IF
GO TO 100-PARA.

300-PARA.
COMPUTE WS-TEMP-1 = WS-TEMP-1 + 1
IF WS-TEMP-1 = WS-INPUT
DISPLAY WS-INPUT 'TH PRIME NO IS:' WS-OUT
GO TO 500-PARA
ELSE
COMPUTE WS-OUT = WS-OUT + 1
MOVE 2 TO WS-TEMP-2
GO TO 100-PARA
END-IF.

500-PARA.
DISPLAY WS-OUT
STOP RUN.
gauravnnl

Posts: 21
Joined: Tue Jul 15, 2014 2:25 pm
Has thanked: 3 times
Been thanked: 0 time

### Re: Nth Prime number

You really should try this without using GO TO. If you start your programming using GO TO like that, you're likely to continue that way, and your programs will be difficult to understand/maintain.

Another clarity issue is meaningful names for things. Give everything a meaningful name (c rubbish-name good-name ALL) and see how much more information your program immediately offers to the reader.
BillyBoyo
Global moderator

Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times