Moved solutions till 35 to Python.

This commit is contained in:
2019-07-16 12:51:07 -04:00
parent f76b36c8d3
commit d94fc90600
13 changed files with 262 additions and 66 deletions

View File

@@ -136,9 +136,10 @@ def collatz_sequence_length(n):
@lru_cache(maxsize=10000)
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
p = 1
for i in range(1, n + 1):
p *= i
return p
def proper_divisors(n):
@@ -177,3 +178,25 @@ def sum_proper_divisors(n):
s += q
d += 1
return s
def permutations(iterable):
"""
Generator that returns all permutations for the iterable.
Generates equivalent result to itertools.permutations.
"""
if not iterable:
yield iterable
for i in range(len(iterable)):
elem = iterable[i:i + 1]
rest = iterable[:i] + iterable[i + 1:]
for ps in permutations(rest):
yield elem + ps
def gcd(a, b):
""" Returns the greatest commond divisor of a and b. """
if b == 0:
return a
return gcd(b, a % b)