MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / isPrime

Function isPrime

other/primelib.py:42–70  ·  view source on GitHub ↗

input: positive integer 'number' returns true if 'number' is prime otherwise false.

(number)

Source from the content-addressed store, hash-verified

40"""
41
42def isPrime(number):
43 """
44 input: positive integer 'number'
45 returns true if 'number' is prime otherwise false.
46 """
47 import math # for function sqrt
48
49 # precondition
50 assert isinstance(number,int) and (number >= 0) , \
51 "'number' must been an int and positive"
52
53 status = True
54
55 # 0 and 1 are none primes.
56 if number <= 1:
57 status = False
58
59 for divisor in range(2,int(round(math.sqrt(number)))+1):
60
61 # if 'number' divisible by 'divisor' then sets 'status'
62 # of false and break up the loop.
63 if number % divisor == 0:
64 status = False
65 break
66
67 # precondition
68 assert isinstance(status,bool), "'status' must been from type bool"
69
70 return status
71
72# ------------------------------------------
73

Callers 5

getPrimeNumbersFunction · 0.70
primeFactorizationFunction · 0.70
goldbachFunction · 0.70
getPrimeFunction · 0.70
getPrimesBetweenFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected