Closest 7-Distinct-Prime Product (via chat)

The OEIS entry A123321 lists the sequence of numbers that are the product of seven distinct primes. For brevity, we'll call this a 7DP number. The first few numbers and their corresponding divisors are below:

510510 = 2 * 3 * 5 * 7 * 11 * 13 * 17
570570 = 2 * 3 * 5 * 7 * 11 * 13 * 19
690690 = 2 * 3 * 5 * 7 * 11 * 13 * 23
746130 = 2 * 3 * 5 * 7 * 11 * 17 * 19


The challenge here will be to find the closest 7DP number, in terms of absolute distance, from a given input.

Input

A single positive integer n in any convenient format.

Output

The closest 7DP number to n, again in any convenient format. If two 7DP numbers are tied for closest, you can output either or both.

Rules


Numbers can be assumed to fit in your language's default [int] datatype (or equivalent).
Either a full program or a function are acceptable.
Standard loopholes are forbidden.
This is code-golf, so all usual golfing rules apply, and the shortest code wins.


Examples

5 -&gt; 510510
860782 -&gt; 870870
1425060 -&gt; 1438710 (or 1411410, or both)
Python, 89 86 85 bytes



f=lambda n,k=0:126^sum(1&gt;&gt;n%i&lt;&lt;7*(n/i%i&lt;1)for i in range(2,n))and f(n+k,k%2*2+~k)or n


The algorithm is O(scary) to begin with and the recursion doesn't really help, but it works well as long as n is sufficiently close to a 7DP number.

Thanks to @xnor for golfing off 3 bytes!

Test it on repl.it.

How it works

Python has no primality or factorization built-ins, but we can identify 7DP numbers by the amount and nature of their divisors.

By the multiplication principle, the number of divisors of an integer can be computed as the product of the incremented exponents of its prime factorization. Thus, &sigma;0(n) (divisor function) is 2m whenever n is a mDP number.

&sigma;0(n) = 128 is thus a necessary condition, but it is not sufficient; for example, &sigma;0(2127) = 128, but 2127 is clearly not a 7DP number. However, if both &sigma;0(n) = 128 and no perfect square divides n evenly, then n is a 7DP number.

For the input n, the algorithm consists in inspecting the integers n, n - 1, n + 1, n - 2, n + 2, etc. and returning the first one that is a 7DP number.

When f is called with argument n, the following happens:


The code

126^sum(1&gt;&gt;n%i&lt;&lt;7*(n/i%i&lt;1)for i in range(2,n))


tests if n is not a 7DP number as follows.

For all integers i such that 1 &lt; i &lt; n, 1&gt;&gt;n%i&lt;&lt;7*(n/i%i&lt;1) gets evaluated.


If n is divisible by i but not by i2, 1&gt;&gt;n%i yields 1 and (n/i%i&lt;1) yields 0, resulting in
1 &middot; 27 &middot; 0 = 1.
If n is divisible by i2, 1&gt;&gt;n%i and (n/i%i&lt;1) both yield 1, resulting in 1 &middot; 27 &middot; 1 = 128.
If n not not divisible by i, 1&gt;&gt;n%i yields 0, resulting in 0 &middot; 27 &middot; x = 0.


&#x200B;
The sum of the resulting integers will be 2m - 2 if n is a mDP number (its 2m divisors, excluding 1 and n) and a number greater than 127 if n has a perfect square factor. Thus, the sum will be 126 if and only if n is a 7DP number.
For 7DP numbers, the sum is 126, so XORing it with 126 yields 0, which is falsy. Thus, the or part of the lambda is executed and f returns the current value of n.
If n is not a 7DP number, XOR will return a non-zero, truthy value. Thus, the and part of the lambda is executed.

f(n+k,k%2*2+~k)


recursively calls f with updated values of n (the next potential 7DP number) and k (the difference between the the new candidate and the one after that).

If k is an even, non-negative integer, k%2*2 yields 0 and ~k yields -(k + 1). The sum of both results is -(k + 1), which is an odd, negative integer that is 1 greater in absolute value than k.

If k is an odd, negative integer, k%2*2 yields 2 and ~k yields -(k + 1). The sum of both results is 2 - (k + 1) = -(k - 1), which is an even, non-negative integer that is 1 unit greater in absolute value than k.

This means that k takes the values 0, -1, 2, -3, 4, &#x22ef;.

When added cumulatively to n0 (the initial value of n), the resulting integers are


n0 + 0
(n0 + 0) - 1 = n0 - 1
(n0 - 1) + 2 = n0 + 1
(n0 + 1) - 3 = n0 - 2
(n0 - 2) + 4 = n0 + 2
etc.


&#x200B;
making sure the first 7DP number we encounter is as close to n0 as possible.