Here they are:
Function no. 1 PRIME NUMBERS FINDER
#find prime numbers less than n
def find_primes(n):
i = 2
primes = []
while i <= n:
if len(find_divisors(i)) == 2:
primes.append(i)
i += 1
else:
i += 1
return primes
print(find_primes(1000))
This function finds all the prime numbers which are less than the given number n. Pretty interesting if you are fascinated by prime numbers.Function no. 2 PRIME FACTORIZATION FUNCTION
#Prime factorization
def factorize(n):
a = n
i = 2
factors = [1]
while i <= n+1:
if (a%i == 0):
factors.append(i)
a = int(a/i)
i = 2
else:
i += 1
return factors
print(factorize(90))
This function returns the prime factorization of a given number. As usual, the item returned is a list.Function no. 3 FIND ALL THE DIVISORS
#Find divisors
def find_divisors(n):
divisors = [1]
i = 2
while i <= n:
if n % i == 0:
divisors.append(i)
i += 1
else:
i += 1
return divisors
print(find_divisors(90))
This function returns the list of the divisors of a given number.Function no. 4 FIND THE MCD
#find MCD
def find_MCD(a,b):
divisors_a = find_divisors(a)
divisors_b = find_divisors(b)
common_divisors = []
for i in divisors_a:
if i in divisors_b:
common_divisors.append(i)
return max(common_divisors)
print(find_MCD(20,40))
This function returns the MCD of two given numbers and can be pretty handy if you need to reduce a fraction. Furthermore, it can easily be extended to more arguments.I wish I could program when I was in high school!! :)
No comments:
Post a Comment