Adi_S - 1 year ago 103

Python Question

`def can_make_product(p, vals):`

if len(vals)==1:

if p==vals[0]:

return True

else:

return False

for i in range(len(vals)):

for k in range(i,len(vals)):

if vals[i] * vals[k]==p:

return True

return False

`p`

`vals`

For example, given

`p=81`

`[2, 2, 3, 3, 4, 9]`

`3×3×9=81`

`true`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

This should work:

```
def can_make_product(p, vals):
# base case empty list: n**0 == 1 for all n
try:
head, tail = vals[0], vals[1:]
except IndexError:
return p == 1
# recursive step: try tail of vals with/without head
if not p % head and can_make_product(p//head, tail):
return True
return can_make_product(p, tail)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**