Project Euler 164 - Numbers for which no three consecutive digits have a sum greater than a given value

Official link: https://projecteuler.net/problem=164

Thought Process

Just a simple recursive function, compute(goal, pos, prev_digit_1, prev_digit_2) takes 4 arguments

  1. goal: how many digits do we want

  2. pos: current digit we are on

  3. prev_digit_1, prev_digit_2: previous 2 digits

if pos = 0, we pick a digit, curr_digit, from 1 to 9 and we add compute(goal, pos + 1, prev_digit_2, curr_digit) to a total

otherwise we pick a digit, curr_digit, from 0 to 9 such that the sum of prev_digit_1, prev_digit_2, and curr_digit is between 0 and 9, and we add compute(goal, pos + 1, prev_digit_2, curr_digit) to a total

I used the @cache decorator from functools to memoize which make the code runnable!

Interactive Code

Enter a number (yourinput)

Code will output the number of yourinput digit numbers (without any leading zero) such that no three consecutive digits have a sum greater than 9