Task : describe a predicate (on Prolog) that count all possible variants of representation of natural number N as product of natural numbers. For example, 6 = 6*1 = 2*3, so answer is 2. The program itself isn't a problem, but main algorithm is hard for me. I get stuck
Asked
Active
Viewed 303 times
0
-
From your example you seem to only count products of two numbers, and the order of the factors is unimportant. Is that correct? – paw88789 May 29 '15 at 20:31
-
@paw88789 yes, but 61 and 16 the same. For my example, answer is 2, not 4.Order of the factors is unimportant. – Simankov May 29 '15 at 20:34
-
Does $24=234$ count, or only products of two factors? – Ross Millikan May 29 '15 at 20:43
-
oh, sorry, my mistake. @RossMillikan you right. – Simankov May 29 '15 at 20:46
-
There is this MSE link for repeated factors admitted and this MSE link for repeated factors not admitted. – Marko Riedel May 29 '15 at 22:26
-
the solution isn't so simple as i expected. Thank for link, @MarkoRiedel – Simankov May 29 '15 at 22:42
1 Answers
2
For a given number $N$ with prime factorization $p_1^{a_1}p_2^{a_2}\cdots p_k^{a_k}$, the total number of positive integer factors is given by the well-known formula, $d(N)=(a_1+1)(a_2+1)\cdots(a_k+1)$.
The factors generally pair up to give $N$ as a product of two factors. For instance, $24=2^3\cdot 3$ has $(3+1)(1+1)=8$ factors, which pair up to give $24=1\cdot 24=2\cdot 12=3\cdot8=4\cdot 6$
However in the case that $N$ is a perfect square, there is one factor (namely $\sqrt{N}$ that pairs with itself.
Thus the number of ways of writing $N$ as a product of two factors is $\left\lceil\frac{d(N)}{2}\right\rceil$, where $\lceil x\rceil$ is the ceiling function of $x$.
paw88789
- 41,907
-
thank you! No doubt, brilliant answer. Unfortunately i mistaken in explanation, so variants like 24 = 432 is possible. Can i use first formula from you answer? – Simankov May 29 '15 at 20:52