From 41f8cc13459de56302bbc848bbdac26bd7fbf069 Mon Sep 17 00:00:00 2001 From: felixm Date: Mon, 2 Oct 2023 20:31:46 +0200 Subject: [PATCH] Solve problem 113. --- python/e113.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 python/e113.py diff --git a/python/e113.py b/python/e113.py new file mode 100644 index 0000000..8788461 --- /dev/null +++ b/python/e113.py @@ -0,0 +1,52 @@ +def is_non_bouncy(n: int): + digits = list(map(int, list(str(n)))) + decreasing = True + increasing = True + for i in range(len(digits) - 1): + increasing = increasing and digits[i] <= digits[i + 1] + decreasing = decreasing and digits[i] >= digits[i + 1] + return increasing or decreasing + + +def non_bouncy_below_naiv(digits: int): + threshold = 10 ** digits + non_bouncy = sum([1 for i in range(1, threshold) if is_non_bouncy(i)]) + return non_bouncy + + +def non_bouncy_below(digits: int): + # increasing + s = [1 for _ in range(1, 10)] + result = sum(s) + for _ in range(digits - 1): + s = [sum(s[i:]) for i in range(9)] + result += sum(s) - 9 + # The -9 is to avoid double counting 11, 22, 33 for increasing and + # decreasing. + + # decreasing + decreasing_count = 0 + s = [i + 1 for i in range(1, 10)] + for _ in range(digits - 1): + # print(s) + decreasing_count += sum(s) + s = [sum(s[:i]) + 1 for i in range(1, 10)] + result += decreasing_count + return result + + +def euler_113(): + assert non_bouncy_below_naiv(6) == 12951 + assert non_bouncy_below(6) == 12951 + assert non_bouncy_below(10) == 277032 + assert is_non_bouncy(134468) + assert is_non_bouncy(66420) + assert (not is_non_bouncy(155349)) + return non_bouncy_below(100) + + +if __name__ == "__main__": + solution = euler_113() + print("e113.py: " + str(solution)) + assert(solution == 51161058134250) +