#Mathematica 136 80 75 bytes
Mathematica 136 80 75 bytes
This is a straightforward approach, working outwards from n
.
n
is a 7-distinct-prime product iff the number of prime factors is 7 (PrimeNu@#==7
) and none of these factors appears more than once (SquareFreeQ@#&
).
g@n_:=(k=1;While[!(PrimeNu@#==7&&SquareFreeQ@#&)⌊z=n-⌊k/2](-1)^k⌋,k++];z)
My earlier submission (136 bytes) found both the first 7-distinct-prime product above n
and, if it exists, the first 7-distinct-prime product below n
. It then simply determined which was closer to n
. If the products were equidistant, it returned both.
The current version checks n-1,n+1,n-2,n+2... until it reaches the first 7-distinct-prime product. This more efficient version adopts the approach Dennis took.
The key advance was in using ⌊k/2](-1)^k⌋
to return the series, 0, 1, -1, 2, -2... The zero is used to check whether n
is itself a 7-distinct-prime product. For this reason, Floor
(that is, ⌊...⌋
) is used instead of Ceiling
.
g[5]
g[860782]
g[1425060]
510510
870870
1438710