12
\$\begingroup\$

Inspired by this question over on Math.

Let the prime factorization of a number, n, be represented as: \$P(n) = 2^a\times3^b\times5^c\times\cdots\$. Then the number of divisors of n can be represented as \$D(n) = (a+1)\times(b+1)\times(c+1)\times\cdots\$. Thus, we can easily say that the number of divisors of \2ドルn\$ is \$D(2n) = (a+2)\times(b+1)\times(c+1)\times\cdots\$,
the number of divisors of \3ドルn\$ is \$D(3n) = (a+1)\times(b+2)\times(c+1)\times\cdots\$,
and so on.

Challenge

Write a program or function that uses these properties to calculate \$n\$, given certain divisor inputs.

Input

A set of integers, let's call them \$w, x, y, z\$, with all of the following definitions:

  • all inputs are greater than 1 -- \$w, x, y, z > 1\$
  • \$x\$ and \$z\$ are distinct -- \$x\ne z\$
  • \$x\$ and \$z\$ are prime -- \$P(x)=x, D(x)=2, P(z)=z \text{ and } D(z)=2\$
  • \$w\$ is the number of divisors of \$xn\$ -- \$D(xn)=w\$
  • \$y\$ is the number of divisors of \$zn\$ -- \$D(zn)=y\$

For the problem given in the linked question, an input example could be \$(28, 2, 30, 3)\$. This translates to \$D(2n)=28\$ and \$D(3n)=30\$, with \$n=864\$.

Output

A single integer, \$n\$, that satisfies the above definitions and input restrictions. If multiple numbers fit the definitions, output the smallest. If no such integer is possible, output a falsey value.

Examples:

(w, x, y, z) => output
(28, 2, 30, 3) => 864
(4, 2, 4, 5) => 3
(12, 5, 12, 23) => 12
(14, 3, 20, 7) => 0 (or some other falsey value)
(45, 13, 60, 11) => 1872
(45, 29, 60, 53) => 4176

Rules:

  • Standard code-golf rules and loophole restrictions apply.
  • Standard input/output rules apply.
  • Input numbers can be in any order - please specify in your answer which order you're using.
  • Input numbers can be in any suitable format: space-separated, an array, separate function or command-line arguments, etc. - your choice.
  • Similarly, if output to STDOUT, surrounding whitespace, trailing newline, etc. are all optional.
  • Input parsing and output formatting are not the interesting features of this challenge.
  • In the interests of sane complexity and integer overflows, the challenge number \$n\$ will have restrictions such that \1ドル < n < 100000\$ -- i.e., you don't need to worry about possible answers outside this range.

Related

caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
asked Jan 15, 2016 at 16:25
\$\endgroup\$
2
  • \$\begingroup\$ So, if the smallest solution is larger than 100,000, I can choose to return either a solution or zero? \$\endgroup\$ Commented Jan 15, 2016 at 18:56
  • \$\begingroup\$ @Dennis If it makes your code shorter, sure. Either would be acceptable. \$\endgroup\$ Commented Jan 15, 2016 at 19:22

1 Answer 1

3
\$\begingroup\$

Jelly, (削除) 17 (削除ここまで) 16 bytes

×ばつ€ȷ5R¤ÆDL€€Z=Ḅi3

This is a brute force solution that tries all possible values up to 100,000. Try it online!

Non-competing version

The latest version of Jelly has a bug fix that allows to golf down the above code to 15 bytes.

×ばつ3ドルÆDL€€=Ḅi3

Try it online!

How it works

×ばつ€ȷ5R¤ÆDL€€Z=Ḅi3 Main link. Left input: x,z. Right input: w,y
 ¤ Combine the two atoms to the left into a niladic chain.
 ȷ5 Yield 100,000 (1e5).
 R Apply range. Yields [1, ..., 100,000].
x€ Multiply each r in the range by x and z.
 This yields [[x, ..., 100,000x], [z, ..., 100,000z]].
 ÆD Compute the divisors of each resulting integer.
 L€€ Apply length to each list of divisors.
 This counts the divisors of each integer in the 2D array.
 Z Zip; group the divisors of kx and kz in pairs.
 = Compare each [divisors(kx), divisors(kz)] with [w, y].
 This yields a pair of Booleans.
 Ḅ Convert each Boolean pair from binary to integer.
 i3 Find the first index of 3. Yields 0 for not found.
answered Jan 15, 2016 at 19:16
\$\endgroup\$
1
  • \$\begingroup\$ Congrats, you win by default! :D \$\endgroup\$ Commented Jan 22, 2016 at 13:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.