mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update playing_with_math.py
This commit is contained in:
parent
3d8721aaeb
commit
6dbd139af1
@ -8,7 +8,6 @@ import random
|
|||||||
|
|
||||||
|
|
||||||
def find_greatest_common_divider(a, b) -> int:
|
def find_greatest_common_divider(a, b) -> int:
|
||||||
'''Implements the greatest common divider algorithm '''
|
|
||||||
|
|
||||||
while(b != 0):
|
while(b != 0):
|
||||||
result = b
|
result = b
|
||||||
@ -18,7 +17,6 @@ def find_greatest_common_divider(a, b) -> int:
|
|||||||
|
|
||||||
|
|
||||||
def _is_prime(number) -> bool:
|
def _is_prime(number) -> bool:
|
||||||
'''Check if a number is prime '''
|
|
||||||
|
|
||||||
if number < 2:
|
if number < 2:
|
||||||
return False
|
return False
|
||||||
@ -31,21 +29,8 @@ def _is_prime(number) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def find_prime_factors(number) -> list:
|
def find_prime_factors(number) -> list:
|
||||||
'''Find prime factors of a number '''
|
|
||||||
|
|
||||||
divisors = [d for d in range(2, number//2 + 1) if number % d == 0]
|
divisors = [d for d in range(2, number//2 + 1) if number % d == 0]
|
||||||
primes = [d for d in divisors if _is_prime(d)]
|
primes = [d for d in divisors if _is_prime(d)]
|
||||||
|
|
||||||
return primes
|
return primes
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
|
||||||
n1 = 21
|
|
||||||
n2 = 7
|
|
||||||
|
|
||||||
print(f'Greatest common divider of {n1} and {n2} is {find_greatest_common_divider(n1, n2)}')
|
|
||||||
print(f'Prime factors of {n1} are {find_prime_factors(n1)}')
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user