Given n ranging from $1$ to $100$, find sum of digits equal to half of arithmetic sum of $1$ to $100$
Using the recursion (thanks Bilou06)
$$f(n, S) = f(n - 1, S) + f(n - 1, S - n),$$
we have the following Python code:
n = 100
S = 2525
matrix = [[0]*(S+1) for x in range(0, n+1)]
for S in range(0, S+1):
matrix[0][S] = 0
for n in range(0, n+1):
matrix[n][0] = 1
for n in range(1, n+1):
for S in range(S+1):
matrix[n][S] = matrix[n-1][S] + matrix[n-1][S-n]
print matrix[n][S]
You can change the values of n
and S
when running the code to verify they work for smaller cases.