Given a natural number n
, we want to know in how many ways we can express these numbers as product of other numbers.
For example, the number 18:
18 = 2 x 9 = 3 x 6 = 2 x 3 x 3 #(3 ways)
See this example, a bit more complicated:
60 = 2 x 30 = 3 x 20 = 4 x 15 = 5 x 12 = 6 x 10 = 2 x 2 x 15 = 2 x 3 x 10 = 2 x 5 x 6 = 3 x 4 x 5 = 2 x 2 x 3 x 5 #(10 ways)
We need to implement the function prod_int_part(), that receives a number n, and outputs the total amount of different products with all the products of max length sorted in this way:
1) each product will be expressed in a list of its factors in increasing order from left to right
2) if there is more than one list-product, these lists should be ordered by the value of the first term, but if two lists have the same term then it should be ordered by the value of the second term.
Let's see some cases:
prod_int_part(18)
== [3, [2, 3, 3]]
prod_int_part(60)
== [10, [2, 2, 3, 5]
If we have only one list-product with the maximum length, there is no use to have it with two nested braces, so the result will be like this case:
prod_int_part(54) == [6, [2, 3, 3, 3]]
Now, let's see examples when n cannot be partitioned:
prod_int_part(37) == [0, []]
prod_int_part(61) == [0, []]
Examples
prod_int_part(60)
prod_int_part(54)
prod_int_part(37)
Enjoy!
This challenge comes from raulbc777 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (1)
Javascript