View previous topic :: View next topic
|
Author |
Message |
mauvis
New User
Joined: 27 Jul 2007 Posts: 11 Location: india
|
|
|
|
Can anyone tell me how we can find the prime number?
i need a simple logic...i need to accept a number through the JCL and have to check through cobol for prime no...
Mauvis |
|
Back to top |
|
|
superk
Global Moderator
Joined: 26 Apr 2004 Posts: 4652 Location: Raleigh, NC, USA
|
|
|
|
A Google search led to this website which has an example program. |
|
Back to top |
|
|
Balraj
New User
Joined: 16 Aug 2007 Posts: 34 Location: Bangalore
|
|
|
|
Logic
Accept A
Perform varying i from 2 by 1 untill i > A
Divide A by i giving c remainder D
if D = 0
Not a prime
stop run
End-if
end-perform
A is a prime no.
stop run. |
|
Back to top |
|
|
himanshupant
New User
Joined: 21 Mar 2007 Posts: 46 Location: India
|
|
|
|
Just an afterthought , most of the times mainframe question / queries have some underlying Business requirement which needs to be satisfied , what was the business requirement behind this...
As for prime number bit , we can repeatedly divide the number ( Say x ) from 2 till x/2 and if in any of the case it divides exactly then its not a prime. We need not divide it till the number x.. This will reduce the number of iterations through loop
Correct me if I am wrong |
|
Back to top |
|
|
guptae
Moderator
Joined: 14 Oct 2005 Posts: 1208 Location: Bangalore,India
|
|
|
|
Hi himanshupant,
Code: |
As for prime number bit , we can repeatedly divide the number ( Say x ) from 2 till x/2 and if in any of the case it divides exactly then its not a prime. We need not divide it till the number x.. This will reduce the number of iterations through loop |
For prime number there is no need to divide till the number x/2 we can repeatedly divide the number til √x |
|
Back to top |
|
|
guptae
Moderator
Joined: 14 Oct 2005 Posts: 1208 Location: Bangalore,India
|
|
|
|
Hi All,
In my prev post i mean till square root of X |
|
Back to top |
|
|
|