## techInterview Discussion |
||

A part of techInterview.org: answers to technical interview questions.
Your host: Michael Pryor |
Given a number, describe an efficient algorithm to find the next number which is prime.
codeislife Sunday, March 12, 2006
This is not a simple easy question.
Take a look at http://en.wikipedia.org/wiki/Prime_testing
This might work. Note: This is not the whole program.
Int number_entered; //Number entered by the user. Int next_prime = number_entered; Int is_prime = 0; Int divisor; While (is_prime == 0 , next_prime++) For (divisor = 2; divisor <= next_prime; divisor++) If (next_prime mod divisor == 0){ If (next_prime == divisor) is_prime = 1; break; } println(“\n\nNext prime: %d”, &next_prime); |

Powered by FogBugz