Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

Julia, 59 bytes

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 64 bytes – efficiency can be improved dramatically.

!n=sort(map(prod,combinations(n>>14+17|>primes,7))-n,by=abs)[]+n

Try it online!

Background

As mentioned in @LuisMendo's answer@LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

How it works

17n|>primes and n>>14+17|>primes (the bitshift is equivalent to dividing by 214 = 16384) compute the prime ranges mentioned in the previous paragraph. Then, combinations(...,7) computes all arrays of seven different prime numbers in that range, and mapping prod over those calculates their products, i.e., the 7DP numbers from which we'll choose the answer.

Next, -n subtracts n prom each 7DP number, then sort(...,by=abs) sorts those differences by their absolute values. Finally, we select the first difference with [] and compute the corresponding 7DP number by adding n with +n.

Julia, 59 bytes

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 64 bytes – efficiency can be improved dramatically.

!n=sort(map(prod,combinations(n>>14+17|>primes,7))-n,by=abs)[]+n

Try it online!

Background

As mentioned in @LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

How it works

17n|>primes and n>>14+17|>primes (the bitshift is equivalent to dividing by 214 = 16384) compute the prime ranges mentioned in the previous paragraph. Then, combinations(...,7) computes all arrays of seven different prime numbers in that range, and mapping prod over those calculates their products, i.e., the 7DP numbers from which we'll choose the answer.

Next, -n subtracts n prom each 7DP number, then sort(...,by=abs) sorts those differences by their absolute values. Finally, we select the first difference with [] and compute the corresponding 7DP number by adding n with +n.

Julia, 59 bytes

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 64 bytes – efficiency can be improved dramatically.

!n=sort(map(prod,combinations(n>>14+17|>primes,7))-n,by=abs)[]+n

Try it online!

Background

As mentioned in @LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

How it works

17n|>primes and n>>14+17|>primes (the bitshift is equivalent to dividing by 214 = 16384) compute the prime ranges mentioned in the previous paragraph. Then, combinations(...,7) computes all arrays of seven different prime numbers in that range, and mapping prod over those calculates their products, i.e., the 7DP numbers from which we'll choose the answer.

Next, -n subtracts n prom each 7DP number, then sort(...,by=abs) sorts those differences by their absolute values. Finally, we select the first difference with [] and compute the corresponding 7DP number by adding n with +n.

added 5 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

Julia, 59 bytes

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 64 bytes – efficiency can be improved dramatically.

!n=sort(map(prod,combinations(17n|>primesn>>14+17|>primes,7))-n,by=abs)[]+n

Try it online!

Background

As mentioned in @LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

How it works

17n|>primes and n>>14+17|>primes (the bitshift is equivalent to dividing by 214 = 16384) compute the prime ranges mentioned in the previous paragraph. Then, combinations(...,7) computes all arrays of seven different prime numbers in that range, and mapping prod over those calculates their products, i.e., the 7DP numbers from which we'll choose the answer.

Next, -n subtracts n prom each 7DP number, then sort(...,by=abs) sorts those differences by their absolute values. Finally, we select the first difference with [] and compute the corresponding 7DP number by adding n with +n.

Julia, 59 bytes

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 64 bytes – efficiency can be improved dramatically.

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

Try it online!

Background

As mentioned in @LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

How it works

17n|>primes and n>>14+17|>primes (the bitshift is equivalent to dividing by 214 = 16384) compute the prime ranges mentioned in the previous paragraph. Then, combinations(...,7) computes all arrays of seven different prime numbers in that range, and mapping prod over those calculates their products, i.e., the 7DP numbers from which we'll choose the answer.

Next, -n subtracts n prom each 7DP number, then sort(...,by=abs) sorts those differences by their absolute values. Finally, we select the first difference with [] and compute the corresponding 7DP number by adding n with +n.

Julia, 59 bytes

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 64 bytes – efficiency can be improved dramatically.

!n=sort(map(prod,combinations(n>>14+17|>primes,7))-n,by=abs)[]+n

Try it online!

Background

As mentioned in @LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

How it works

17n|>primes and n>>14+17|>primes (the bitshift is equivalent to dividing by 214 = 16384) compute the prime ranges mentioned in the previous paragraph. Then, combinations(...,7) computes all arrays of seven different prime numbers in that range, and mapping prod over those calculates their products, i.e., the 7DP numbers from which we'll choose the answer.

Next, -n subtracts n prom each 7DP number, then sort(...,by=abs) sorts those differences by their absolute values. Finally, we select the first difference with [] and compute the corresponding 7DP number by adding n with +n.

added 675 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

Julia, 5259 bytes

!n=minabsn=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)+n[]+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 5764 bytes – efficiency can be improved dramatically.

!n=minabsn=sort(map(prod,combinations(n>>14+17|>primes17n|>primes,7))-n,by=abs)+n[]+n

Try it online!Try it online!

Background

As mentioned in @LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

How it works

17n|>primes and n>>14+17|>primes (the bitshift is equivalent to dividing by 214 = 16384) compute the prime ranges mentioned in the previous paragraph. Then, combinations(...,7) computes all arrays of seven different prime numbers in that range, and mapping prod over those calculates their products, i.e., the 7DP numbers from which we'll choose the answer.

Next, -n subtracts n prom each 7DP number, then sort(...,by=abs) sorts those differences by their absolute values. Finally, we select the first difference with [] and compute the corresponding 7DP number by adding n with +n.

Julia, 52 bytes

!n=minabs(map(prod,combinations(17n|>primes,7))-n)+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 57 bytes – efficiency can be improved dramatically.

!n=minabs(map(prod,combinations(n>>14+17|>primes,7))-n)+n

Try it online!

Background

As mentioned in @LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

Julia, 59 bytes

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

This is very inefficient, but it works for the first test case in practice and for the others in theory.

At the cost of 5 more bytes – for a total of 64 bytes – efficiency can be improved dramatically.

!n=sort(map(prod,combinations(17n|>primes,7))-n,by=abs)[]+n

Try it online!

Background

As mentioned in @LuisMendo's answer, the set of primes we have to consider for the nearest 7DP number is quite small. It suffices for the set to contains a 7DP number that is bigger than the input n, which will be true if and only if it contains a prime p ≥ 17 such that 30300p = 2·3·5·7·11·13·p ≥ n.

In On the interval containing at least one prime number proves that the interval [x, 1.5x) contains at least one prime number whenever x ≥ 8. Since 30030 / 16384 ≈ 1.83, that means there must be a prime p in (n/30030, n/16384) whenever n > 8 · 30300 = 242400.

Finally, when n < 510510, p = 17 is clearly sufficient, so we only need to consider primes up to n/16384 + 17.

At the cost of efficiency, we can consider primes up to 17n instead. This works when n = 1 and is vastly bigger than n/16384 + 17 for larger values of n.

How it works

17n|>primes and n>>14+17|>primes (the bitshift is equivalent to dividing by 214 = 16384) compute the prime ranges mentioned in the previous paragraph. Then, combinations(...,7) computes all arrays of seven different prime numbers in that range, and mapping prod over those calculates their products, i.e., the 7DP numbers from which we'll choose the answer.

Next, -n subtracts n prom each 7DP number, then sort(...,by=abs) sorts those differences by their absolute values. Finally, we select the first difference with [] and compute the corresponding 7DP number by adding n with +n.

added 1162 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading