Skip to main content
2 of 5
adopted Dennis' more efficient approach. Only one product is returned.
DavidC
  • 25.4k
  • 2
  • 52
  • 105

#Mathematica 136 80 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-Ceiling[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 Ceiling[k/2](-1)^k] to produce the series, 1, -1, 2, -2...


g[5]
g[860782]
g[1425060]

510510

870870

1438710

DavidC
  • 25.4k
  • 2
  • 52
  • 105