Ans.: The user will input a positive integer and the method should output “Prime” or “Not Prime” based on whether the input integer is a prime number or not.
The logic is to find a positive integer less than or equal to the square root of input integer. If there is a divisor of number that is less than the square root of number, then there will be a divisor of number that is greater than square root of number. Hence, we have to traverse till the square root of number.
The time complexity of this function is O(√N) because we traverse from 1 to √N.
- input: 20, output: Not Prime
- input: 17, output: Prime
No comments:
Post a Comment