# [Community Puzzle] Count as I count #2

Coding Games and Programming Challenges to Code Better

Created by @BLANC,validated by @Timinator,@Blubbor and @The_Goat_2000.
If you have any issues, feel free to ping them.

Hi, my code works fine with 1,2 and 3rd validator but starts failing after. i checked until 40 and it seems correct, after it begins to be too hard to check :/. Can anyone spot if i’m missing something ?
Here is my sol it’s very short, (i’m sort of proud couse i didnt “calculate” anything except for the sum, i found the “closed combinatoric form”):

``````def find_sum (list_number, target): #i try every combinations and if its sum is == target i save it
results = []
for x in list_number:
results.extend([combo for combo in combinations_with_replacement(list_number,x)
if sum(combo) == target])
return results
``````

after the calculation of all possible sum i used this formula to find every coefficients for each solution

``````def find_coefficient(lista_soluzioni):    # formula of permutation with repetition modified with if-clause on the numerator
lista_coefficienti = []
for soluzione in lista_soluzioni:
denominatore = 1 #inizialize the denominator
for x in set(soluzione):
denominatore *= math.factorial(soluzione.count(x))
if 1 in soluzione :
lista_coefficienti.append(int((math.factorial(len(soluzione))* 2**(len(soluzione) - soluzione.count(1)) / denominatore)))
else :
lista_coefficienti.append(int((math.factorial(len(soluzione)) * 2**(len(soluzione)) / denominatore)))
return lista_coefficienti
``````

the trick is that when i found the sum with VALUE_pins 1 to 12, here i also have the solutions with the DOWN_pins 2 to 12!!! i only have to throw away all the solutions that inlcude 1. So i (believe) figured out that the combinatoric for each distinct solution is simply : permutations with repetitions (formua → n!/k_1!..k_r! where k_1 … k_r are the numbers of time that a given number appear in the solution) multiplied by 2**s where s depend if in the solution there is the 1 or not: if there is not → s= len(solution), if there are → s = len(solution) - len(count(1)). The 2 arise from the fact that for every number <> 1 in the sulotion i have 2 “copies” of it. Tricky but very fast.

i give u some output for 44 → target = 6

ALL SOLUTIONS
[(6,), (1, 5), (2, 4), (3, 3), (1, 1, 4), (1, 2, 3), (2, 2, 2), (1, 1, 1, 3), (1, 1, 2, 2), (1, 1, 1, 1, 2), (1, 1, 1, 1, 1, 1)]
COEFFICIENTS FOR EACH SOLUTION
[2, 4, 8, 4, 6, 24, 8, 8, 24, 10, 1]
TOTAL COMBINATIONS
99

41 → target 9
[(9,), (1, 8), (2, 7), (3, 6), (4, 5), (1, 1, 7), (1, 2, 6), (1, 3, 5), (1, 4, 4), (2, 2, 5), (2, 3, 4), (3, 3, 3), (1, 1, 1, 6), (1, 1, 2, 5), (1, 1, 3, 4), (1, 2, 2, 4), (1, 2, 3, 3), (2, 2, 2, 3), (1, 1, 1, 1, 5), (1, 1, 1, 2, 4), (1, 1, 1, 3, 3), (1, 1, 2, 2, 3), (1, 2, 2, 2, 2), (1, 1, 1, 1, 1, 4), (1, 1, 1, 1, 2, 3), (1, 1, 1, 2, 2, 2), (1, 1, 1, 1, 1, 1, 3), (1, 1, 1, 1, 1, 2, 2), (1, 1, 1, 1, 1, 1, 1, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1)]
COEFFICIENTS FOR EACH SOLUTION
[2, 4, 8, 8, 8, 6, 24, 24, 12, 24, 48, 8, 8, 48, 48, 96, 96, 64, 10, 80, 40, 240, 80, 12, 120, 160, 14, 84, 16, 1]
TOTAL COMBINATIONS
1393

Thanks very much !