README:
Steps followed:
- First, asked the user to provide a positive integer to check whether it is a prime.
- Stored the input number into an integer variable.
- Execution goes to the isPrime() function where we are returning false initially if the number is either 0 or 1.
- Then calculating the square root of the number to loop until that root.
- Applying a for loop starting from 2 to check whether it is divisible by the numbers in between or not.
- Returning true or false based on the divisibility.
- In main function, printing the output of the bool function.
Optimizations:
-
Instead of looping through the (input number)/2, I am only looping through it's square root as it can be sufficient to check for the prime hence decreasing the compile time.
-
Have searched along many optimization tricks and codes to only use them as reference, but they are for numbers far exceeding the integer range or finding primes within a range with speed. I came to a conclusion to not include any of those as it is increasing the complexity unnecesorrily and we can find whether the number is prime or not in rather simple steps with efficient speed and memory. Hence, I have tried to complete the code in as lesser lines as possible.