java - what this method does - interview -
The following is an interview question ... how can you start solving this kind of question? Is there a general algorithm for such questions?
The question is what is this method. I know that he gives some inputs that I've tried (and it's okay), but I do not know how to start to solve such questions ...
public boolean key (int num) {boolean ans = true; For (int x = 2; (x * x & lt; = num) & amp; ans; x = x + 1) {if ((num% x) == 0) ans = false; } Return ans; } thx!
Provide some sample input, and get output by which you can follow the reverse energizing process You can set the result.
Choose your input wisely so that it is easy to solve this problem.
Currently -
Input: 1 Return: Right / Input: 2 Return: True - Input: 3 Returns: True. Input: 4 Returns: False Input: 5 Returns: True ... ... Input: 9 Returns: False // Aha .. This seems to be the major number ... ... Input: 111 Returns: False
Then its main number in this case logic.
Comments
Post a Comment