Comments on Prime Difference
Parent
Prime Difference
Given an integer n, output the smallest prime such that the difference between it and the next prime is at least n.
For example, if n=5
, you would output 23
, since the next prime is 29
, and 29-23>=5
.
More Input/Output Examples
1 -> 2 (3 - 2 >= 1)
2 -> 3 (5 - 3 >= 2)
3 -> 7 (11 - 7 >= 3)
4 -> 7
5 -> 23
6 -> 23
7 -> 89
8 -> 89
This is code-golf, so shortest code wins.
[Husk], 8 bytes Ψḟo≥⁰≠İ …
4y ago
[Dyalog APL Extended], 14 byte …
4y ago
[Raku], 33 bytes {1 …
4y ago
Ruby, 56 bytes ```ruby ->n …
3y ago
Japt, 15 14 bytes @§XnÈ …
3y ago
C (gcc), 126 129 bytes ```c …
4y ago
[JavaScript (Node.js)], 81 byt …
4y ago
[JavaScript (Node.js)], 86 byt …
4y ago
Post
Husk, 8 bytes
Ψḟo≥⁰≠İp
Try it online! or Verify first 8 values
It is always a good day when you get to use Ψ
in your program.
Explanation
Ψḟo≥⁰≠İp
İp to the infinite list of prime numbers,
Ψ apply this higher order function on overlapping pairs
ḟo first element where
≠ absolute difference
≥⁰ is greater than or equal to the input.
1 comment thread