## Sunday, July 19, 2020

Solution Exercise 67: prim number in Python

#### Exercise 67

Write a function in Python which takes as argument an integer n and which returns True if the number n is prime and False if n is not prime without using any predefined function.

#### Solution

``````def primNumber(n):
# take initial value of number of divisors
numberOfDivisors = 0
for i in range(1,n+1):

# while i is a divisor of n we increment we increment numberOfDivisors
if n%i == 0:
numberOfDivisors = numberOfDivisors + 1
# the integer n is a prim number only if numberOfDivisors == 2
if numberOfDivisors == 2:
return True
else:
return False

# Testing the function
print(primNumber(7)) # The output is True because 7 is a prim number
print(primNumber(6)) # The output is False because 6 is not a prim number
``````

Younes Derfoufi
my-courses.net

## Python Exercises With Solutions

1. Python Arithmetic Exercises
2. Equations & System Of Equations
3. Polynomials and Functions
4. Python Arithmetic Exercises
5. Numpy, Matplolib, Sympy , Scipy - Exercises
6. Python Arithmetic Exercises

Younes Derfoufi

## Sample Text 