P41, P42, P43, P45, P46, P47, P48, P50, P52, P53, P55, P56: Added Python solutions.

parent aa82c55e
 ... ... @@ -54,6 +54,19 @@ ANSWERS = { 38: "932718654", 39: "840", 40: "210", 41: "7652413", 42: "162", 43: "16695334890", 45: "1533776805", 46: "5777", 47: "134043", 48: "9110846700", 50: "997651", 52: "142857", 53: "4075", 55: "249", 56: "972", 57: "153", } ... ...
p041.py 0 → 100644
 # # Solution to Project Euler problem 41 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # import eulerlib NONPRIME_LAST_DIGITS = set([0, 2, 4, 5, 6, 8]) def compute(): for i in range(9, 0, -1): arr = list(range(i, 0, -1)) while True: if i == 1 or arr[-1] not in NONPRIME_LAST_DIGITS: n = int("".join([str(x) for x in arr])) if eulerlib.is_prime(n): return str(n) if not prev_permutation(arr): break def prev_permutation(arr): i = len(arr) - 1 while i > 0 and arr[i - 1] <= arr[i]: i -= 1 if i <= 0: return False j = len(arr) - 1 while arr[j] >= arr[i - 1]: j -= 1 arr[i - 1], arr[j] = arr[j], arr[i - 1] arr[i : ] = arr[len(arr) - 1 : i - 1 : -1] return True if __name__ == "__main__": print(compute())
p042.py 0 → 100644
p043.py 0 → 100644
 # # Solution to Project Euler problem 43 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # def compute(): ans = 0 num = list(range(10)) while True: if is_substring_divisible(num): ans += int("".join(map(str, num))) if not next_permutation(num): break return str(ans) DIVISIBILITY_TESTS = [2, 3, 5, 7, 11, 13, 17] def is_substring_divisible(num): for i in range(7): x = num[i + 1] * 100 + num[i + 2] * 10 + num[i + 3] if x % DIVISIBILITY_TESTS[i] != 0: return False return True def next_permutation(arr): i = len(arr) - 1 while i > 0 and arr[i - 1] >= arr[i]: i -= 1 if i <= 0: return False j = len(arr) - 1 while arr[j] <= arr[i - 1]: j -= 1 arr[i - 1], arr[j] = arr[j], arr[i - 1] arr[i : ] = arr[len(arr) - 1 : i - 1 : -1] return True if __name__ == "__main__": print(compute())
p045.py 0 → 100644
 # # Solution to Project Euler problem 45 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # def compute(): i = 286 j = 166 k = 144 while True: triangle = i * (i + 1) // 2 pentagon = j * (j * 3 - 1) // 2 hexagon = k * (k * 2 - 1) minimum = min(triangle, pentagon, hexagon) if minimum == triangle and minimum == pentagon and minimum == hexagon: return str(triangle) if minimum == triangle: i += 1 if minimum == pentagon: j += 1 if minimum == hexagon : k += 1 if __name__ == "__main__": print(compute())
p046.py 0 → 100644
 # # Solution to Project Euler problem 46 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # import eulerlib, itertools def compute(): for n in itertools.count(9, 2): if not test_goldbach(n): return str(n) def test_goldbach(n): if n % 2 == 0 or eulerlib.is_prime(n): return True for i in itertools.count(1): k = n - 2 * i * i if k <= 0: return False elif eulerlib.is_prime(k): return True if __name__ == "__main__": print(compute())
p047.py 0 → 100644
 # # Solution to Project Euler problem 47 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # import eulerlib, itertools def compute(): for i in itertools.count(): for j in range(4): if count_distinct_prime_factors(i + j) != 4: break else: return str(i) distinctprimefactorscache = {} def count_distinct_prime_factors(n): if n not in distinctprimefactorscache: count = 0 while n > 1: count += 1 k = smallest_prime_factor(n) n //= k while n % k == 0: n //= k distinctprimefactorscache[n] = count return distinctprimefactorscache[n] def smallest_prime_factor(x): for i in range(2, eulerlib.sqrt(x) + 1): if x % i == 0: return i return x if __name__ == "__main__": print(compute())
p048.py 0 → 100644
 # # Solution to Project Euler problem 48 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # def compute(): MOD = 10**10 ans = sum(pow(i, i, MOD) for i in range(1, 1001)) % MOD return str(ans) if __name__ == "__main__": print(compute())
p050.py 0 → 100644
 # # Solution to Project Euler problem 50 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # def compute(): # Sieve of Eratosthenes isprime = [True] * 1000000 isprime[0] = isprime[1] = False for i in range(len(isprime)): if isprime[i]: for j in range(i * i, len(isprime), i): isprime[j] = False primes = [n for (n, x) in enumerate(isprime) if x] ans = 0 consecutive = 0 for i in range(len(primes)): sum = primes[i] consec = 1 for j in range(i + 1, len(primes)): sum += primes[j] consec += 1 if sum >= len(isprime): break if isprime[sum] and consec > consecutive: ans = sum consecutive = consec return str(ans) if __name__ == "__main__": print(compute())
p052.py 0 → 100644
 # # Solution to Project Euler problem 52 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # import itertools def compute(): for i in itertools.count(1): digits = list(sorted(str(i))) for j in range(2, 7): temp = list(sorted(str(i * j))) if temp != digits: break else: return str(i) if __name__ == "__main__": print(compute())
p053.py 0 → 100644
 # # Solution to Project Euler problem 53 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # import eulerlib def compute(): ans = 0 for n in range(1, 101): for k in range(0, n + 1): if binomial(n, k) > 1000000: ans += 1 return str(ans) def binomial(n, k): return eulerlib.factorial(n) // (eulerlib.factorial(k) * eulerlib.factorial(n - k)) if __name__ == "__main__": print(compute())
p055.py 0 → 100644
 # # Solution to Project Euler problem 55 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # def compute(): ans = 0 for i in range(10000): if is_lychrel(i): ans += 1 return str(ans) def is_lychrel(n): for i in range(50): n += int(str(n)[::-1]) if str(n) == str(n)[::-1]: return False return True if __name__ == "__main__": print(compute())
p056.py 0 → 100644
 # # Solution to Project Euler problem 56 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # def compute(): ans = 0 for a in range(100): for b in range(100): ans = max(sum([int(c) for c in str(a**b)]), ans) return str(ans) if __name__ == "__main__": print(compute())
p057.py 0 → 100644
 # # Solution to Project Euler problem 57 # by Project Nayuki # # http://www.nayuki.io/page/project-euler-solutions # https://github.com/nayuki/Project-Euler-solutions # def compute(): ans = 0 numer = 0 denom = 1 for i in range(1000): numer, denom = denom, denom * 2 + numer if len(str(numer + denom)) > len(str(denom)): ans += 1 return str(ans) if __name__ == "__main__": print(compute())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!