Efficient algorithm for finding the number within an interval given its prime factors

84 Views Asked by At

Suppose we are given two integers $\ell$ and $h$ such that $\ell \leq h$ and a list of distinct prime numbers $P = [p_1,p_2,\dots,p_n]$ (sorted in ascending order). We are interested in finding an integer $a$ (if any) such that:

  1. the prime factors of $a$ are every element in $P$ (mathematically $a = p_1^{e_1}p_2^{e_2} \dots p_n^{e_n}$ for some $e_i \geq 1$ for any $1 \leq i \leq n$),
  2. $a$ is between $\ell$ and $h$ (i.e., $\ell \leq a \leq h$).

Here, we just want a value of $a$, but it would be nice if $a$ is the smallest number satisfying the condition. My attempts so far are as follows. Define $f = \prod_{i=1}^{n}p_i$, checks if $\ell \leq f \leq h$. If $h < f$, the integer $a$ does not exist. If $f < \ell $, I consider two options below:

  1. We multiply $f$ by $p_1$ (the smallest prime in the list) repeatedly until we get an expression $f \cdot p_1^{e_1} \geq \ell $. However, this approach does not always work (see the example below).
  2. We compute $e = \lfloor \log_{f}\ell \rfloor$ and define $p_1 \cdot f^e$. We sometimes have $p_1 \cdot f^e \geq \ell$ or $ \ell \leq f^{(e+1)} \leq h $, but unfortunately, this approach does not always work (see the example below).

The counterexample for both methods above is as follows. Let $\ell = 1000$, $h = 1100$, and $P = [3,7]$. We have $f = 3 \cdot 7 = 21$. If we use the first approach, we have the sequence $21, 63, 189, 567, 1701$; we do not find $a$ satisfying the condition. Using the second approach, we have $e = \lfloor \log_{21}1000 \rfloor = 2$. We do not find the solution because we have $3 \cdot f^e = 3 \cdot 21^2 = 1323 > 1100 $ and also $f^{(e+1)} = 21^3 = 9261$. However, a solution to this problem exists, i.e., $a = 3 \cdot 7 \cdot 7 \cdot 7 = 1029$.

Another approach is as follows, but I think it is not efficient and more cumbersome:

  1. Define $g = \prod_{i=1}^{n}p_i^{d_i}$, initially all $d_i$ are set to $1$. We also define $f = \prod_{i=1}^{n}p_i$ as above. Notice that initially $g = f$.
  2. Compute $b = \lceil \log_f h\rceil $. Observe that $f^b > h$.
  3. Increment each $d_i$ one-by-one, i.e., the tuple $(d_1,d_2,\dots,d_n)$ is incremented in lexicographic order. Each value $d_i$ is between $1$ and $b$ (inclusive).
  4. The iteration terminates if we find $g$ such that $\ell \leq g \leq h$ or we obtain the condition $g > h$ (we have no solution).

However, in the last algorithm, there are at most $b$ iterations for each $d_i$; thus, it is inefficient.

Any idea to solve this efficiently? Or perhaps such a problem is at least as hard as the integer factorization problem?

Addendum: The problem does not seem easy and is probably NP-hard. Is there any way to prove the NP-hardness of such a problem? Is it possible to determine the exact computational complexity class of this problem?