What’s here -:

Step 1: Understanding the Problem
We need to check if a given number is prime or not. A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself.
Step 2: Developing the Algorithm
To check if a number is prime, we can iterate from 2 to the square root of the number (inclusive) and check if any of these values divide the number evenly. If we find any such divisor, the number is not prime. Otherwise, it is prime.
Step 3: Writing the Python Code
Here’s the Python code to check if a number is prime:
import math
def is_prime(number):
if number <= 1:
return False
for divisor in range(2, int(math.sqrt(number)) + 1):
if number % divisor == 0:
return False
return True
# Test the function
number = 29
if is_prime(number):
print(number, "is a prime number.")
else:
print(number, "is not a prime number.")
O/P
29 is a prime number.
Time Complexity: O(root(n))
Step 4: Code Explanation
1. We import the math module to use the sqrt function for calculating the square root of the number efficiently.
2. The function is_prime takes the number as an argument and checks if it is prime or not.
3. If the number is less than or equal to 1, we return False since prime numbers must be greater than 1.
4. We iterate from 2 to the square root of the number (inclusive) using a for loop.
5. Inside the loop, we check if the number is divisible evenly by the current divisor. If so, we return False since it is not prime.
6. If none of the divisors divide the number evenly, we return True, indicating that the number is prime.
Step 5: Dry Run the Code
Let’s do a dry run of the code iteration-wise for number = 29:
Iteration 1:
divisor is 2.
29 is not divisible evenly by 2.
Iteration 2:
divisor is 3.
29 is not divisible evenly by 3.
Iteration 3:
divisor is 4.
29 is not divisible evenly by 4.
Iteration 4:
divisor is 5.
29 is not divisible evenly by 5.
Since we have iterated through all the possible divisors up to the square root of 29 (which is 5), and none of them divide the number evenly, we can conclude that 29 is a prime number.
The code will return True and print “29 is a prime number.”
This iteration-wise dry run demonstrates that the code checks each possible divisor iteratively to determine if the number is prime or not. In the case of 29, the code confirms that it is indeed a prime number.
Please note that for smaller numbers, the loop may have fewer iterations depending on their factors.
Feel free to modify and experiment with the code to understand it better and explore other variations or optimizations.
