Sravani
Answered On : Aug 29th, 2012
One of the logic is:
Check the number for divisibility from 2 to till the number and if the reminder is 0 increment the counter and finally check if the counter>1 the number is not Prime.
Login to rate this answer.
1. You dont need to loop further than the integer of the square root of the number in question
2. You can skip multiples of numbers already checked (if a number is not divisible by 2 then its not by 4 either)
Login to rate this answer.
Gunesh Asatkar
Answered On : Feb 13th, 2013
set the start and end limit from which prime will calculate.
then start first for loop and second for loop.
Code
DATA DIVISION.
WORKING-STORAGE SECTION.
77 I PIC 999 VALUE 001.
77 N PIC 999 VALUE 030.
77 M PIC 999 VALUE 1.
77 A PIC 999.
77 Q PIC 999.
77 R PIC 999.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "PRIME NUMBER GENERATION".
DISPLAY "PRIME NUMBER START FROM" I.
DISPLAY "PRIME NUMBER END " N.
PERFORM LOOP UNTIL I>N.
GO TO STOP-PARA.
LOOP.
PERFORM VARYING K FROM 1 BY 1 UNTIL K > I
DIVIDE I BY K GIVING Q REMAINDER R
IF R = 0
COMPUTE A =A+1
END-IF
END-PERFORM.
IF A = 2
DISPLAY I
END-IF
MOVE 0 TO A
COMPUTE I = I+1.
STOP-PARA.
STOP RUN.
Login to rate this answer.