Combination of numbers of multiplications

Question:

I am currently coding in python a special program. I want to find out what combinations of numbers from a list gives me my answer, it is quite hard to explain so here are some examples:

Example 1

Number = 18
List = [2, 3]

I want the output to be: 3^2 * 2 = 18

Example 2

Number = 30
List = [2, 3, 5]

I want the output to be: 3 * 2 * 5 = 30

Example 3

Number = 20
List = [2, 5]

I want the output to be: 2^2 * 5 = 20

I have been at this problem for about 4 hours and I just can’t seem to figure it out! Any help is appreciated! Thanks!

Asked By: John Drew

||

Answers:

If you want to do it with just with standard python you can use this (which does not give you an error if the number can not be represented by your factors):

number = 20
factors = [2, 5]
for factor in factors:
    while number % factor == 0:
        print(factor)
        number = number / factor

Do you want to do prime factorization instead?

Answered By: bitflip

edit: Assuming you mean a countdown solver. It was pointed out by another user that you might just be looking for prime factorisation.

As mentioned above, this is called a "countdown solver".

You could try this approach (I also encourage others to improve upon this solution!)

In this example I’ll be calling the numbers in your list ‘option numbers’.

  1. Take target number, perform one of the mathematical functions on it using one of your option numbers. (Add a number, take a number, divide, multiply etc).
  2. Check to see if the result is a product, factor, or is reachable through addition/subtraction/powers of any of the other option numbers.
  3. If you can’t reach it, recurse with your new number as the target.

This assumes you can reuse option numbers, which you have in your example above.

Answered By: Someone Else

Try this one. Works for python 3.9+. I’ve also shared the workaround for versions less than 3.9 below.

from collections import Counter
from math import lcm

def get_repr(num, factors):
  factors = list(factors)
  f_lcm = lcm(*factors)
  
  if (num % f_lcm != 0):
    return "Could not be represented by given factors."
  
  factors.append(num // f_lcm)
  c = Counter(factors)
  string = []
  for k, v in c.items():
    if (k == 1):
      continue
      
    if (string):
      string.append(' * ')
      
    string.append(str(k))
    if (v > 1):
      string.append('^')
      string.append(str(v))
  return ''.join(string)
  
print(get_repr(20, [2, 5]))

# Output
# 2^2 * 5

For python version less than 3.9, here’s the workaround for lcm function.

from math import gcd
from functools import reduce

def lcm(*factors):
  return reduce(lambda a, b: a * b // gcd(a, b), factors)
Answered By: Maya
import math 
def check_reach_mul(list, num):
    num_list = [math.prod(list)]
    for idx, item in enumerate(list):
        num_list.append(math.prod([i if i!=item else pow(item,2) for i in list ]))
    return num in num_list

print(check_reach_mul([2, 3], 18)) #True
print(check_reach_mul([2,3,5], 30)) #True
print(check_reach_mul([2, 5], 20)) #True
Answered By: uozcan12
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.