Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Here is a tricky program which can be good for exam
But is there any more simple logic to code that i found a logic here but seems difficult

http://fahad-cprogramming.blogspot.com/2013/02/find-last-prime-number-occur-before.html[^]


Please Share If Anyone Got..

Thanks For Your Time..
Posted
Comments
_Maxxx_ 12-Jan-14 21:17pm    
If you think that the code you liked to is difficult then I think you should steer clear of this sort of thing - it's hardly possible to write a more simple program!
this is a pure 'brute-force' approach to finding a prime.
There are lots of ways to find primes that are more efficient - a quick google will provide tons of examples - but the code would be more complex - and if you're finding the given example difficult you may not want to look
Can't you simplify it yourself? If you face problem in between, then come back and ask a question.
Mohibur Rashid 12-Jan-14 23:27pm    
if this one is complicated for you................
[no name] 12-Jan-14 23:41pm    
Can you see a pattern in the comments? Bit tricky.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

And if you find that to have "difficult logic" then I suspect you are about to be completely overwhelmed, and should perhaps consider a different career...because that is a brute force, no finesse simple method: you can't get much simpler, though you can get a lot more elegant.
 
Share this answer
 
Man, that algorithm is really simple: for the given input n it searches, starting from (n-1) downwards for a prime number (this is the outer loop).
For every prime-number-candidate it uses the prime number definition for testing primality: repeadly attempts to find an exact divisor (that would mean 'not a prime number', you know). That's performed by the inner loop.
There are more efficient algorithms but that is possibly the simplest one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900