| Bytes | Lang | Time | Link |
|---|---|---|---|
| 091 | Desmos | 250726T010111Z | ErikDaPa |
| 012 | Japt | 250725T144122Z | Shaggy |
| 012 | Pyth | 250724T212921Z | Synoli |
| 024 | Charcoal | 250719T125608Z | Neil |
| 067 | Retina 0.8.2 | 250719T113058Z | Neil |
| 029 | APLNARS | 250718T074715Z | Rosario |
| 059 | Ruby rprime | 250718T220038Z | Value In |
Desmos, 91 bytes
a=[i-sign(∑_{n=2}^{i-1}0^mod(i,n))ifori=[x...y]]
b=a[a>0] => filter out non-primes in range
f(x,y)=b[1...b.count-1]-b[2...] => find pairwise deltas
Outputs an empty list if there are no primes in the range.
Try this online!
Pyth, 17 13 12 bytes
.+f|qT1P_T}F
Explanation
.+ # Calculate deltas between consecutive elements of a sequence …
f # … which is the result of a filter operation …
| # … which tests if one of the following conditions is true:
qT1 # T == 1?
P_T # T is prime?
} # Apply that filter operation to a range …
F # … whose start and end are defined as the first two items of …
Q # … the input, evaluated as an expression. (Q is implied)
Change log
Replaced
#withf, which allowed me to inline the lambda. (−2 bytes)Replaced
AQ[…]GHwith.*Qand leverage the fact that a trailingQis implied. (−2 bytes)Replaced
.*withF, which is equivalent for binary functions according to this tip. (−1 byte)
Charcoal, 24 bytes
≔Φ…·NN⬤…²ι﹪ιλυIEΦυκ⁻ι§υκ
Try it online! Link is to verbose version of code. Explanation:
≔Φ…·NN⬤…²ι﹪ιλυ
Filter out the composite numbers from the input range.
IEΦυκ⁻ι§υκ
Output the consecutive differences.
Retina 0.8.2, 67 bytes
\d+
$*
M&!`(?<=(1+),1*)1*\1
O`
A`^(..+)\1+$
M!`(?<=\b\1¶(1+))1+
%`1
Try it online! Link is to test suite that joins the output on commas for convenience. Outputs 0 if there are no prime distances. Explanation:
\d+
$*
Convert to unary.
M&!`(?<=(1+),1*)1*\1
List the numbers in the range in descending order.
O`
Sort.
A`^(..+)\1+$
Delete composite numbers, keeping 1 and prime numbers.
M!`(?<=\b\1¶(1+))1+
Calculate the forward differences.
%`1
Convert to decimal, but if there were no differences, output 0.
(This challenge seems strangely suited to Retina, where it's easier to find composites rather than primes, and to output 0 if the list is empty.)
APL(NARS), 29 chars
{1↓⌽2-/⌽0,w/⍨(0πw)∨1=w←↑../⍵}
⍬ zilde is print as
┌0─┐
│ 0│
└~─┘
and would be a list of numbers [~] with 0 elements.
There was a little problem if i pass zilde to the function 2-/ I obtain
2-/⍬
DOMAIN ERROR
2-/⍬
∧
while all seems ok for one list of at last one element
2-/,1
┌0─┐
│ 0│
└~─┘
that return zilde. So for not make out the Domain Error and gain some chars, I simply add one
value in the array pass to 2-/ [2-/⌽0,...] function, and drop the first value [1↓...]
in the end result, because
1↓⍬
┌0─┐
│ 0│
└~─┘
not generate error and return zilde (in the case 2-/ has input only one array of one element and return zilde)
test:
f←{1↓⌽2-/⌽0,w/⍨(0πw)∨1=w←↑../⍵}
f 1 12
┌5─────────┐
│ 1 1 2 2 4│
└~─────────┘
f 21 31
┌2───┐
│ 6 2│
└~───┘
f 80 100
┌2───┐
│ 6 8│
└~───┘
f 84 88
┌0─┐
│ 0│
└~─┘
f 5 5
┌0─┐
│ 0│
└~─┘
Ruby -rprime, 59 bytes
->a,b{[1,*Prime.each(b)].grep(a..).each_cons(2).map{_2-_1}}