1
\$\begingroup\$

Task:

Write a code golf program that, given two positive integers n and m, returns a list of the distances between consecutive prime numbers in the range [n, m] inclusive.

Note: My question is different from this one

Assumptions:

  • 1 is considered the first prime number
  • n < m
  • Both n and m are inclusive
  • Code golf scoring

Examples:

 - Input: 1, 12 → Output: [1, 1, 2, 2, 4] (Distance between consecutive primes: 1-2, 2-3, 3-5, 5-7, 7-11)
 - Input: 21, 31 →   Output: [6, 2]
 - Input: 80, 100 → Output: [6, 8]
 - Input: 84, 88 → Output: [0] (No primes in range)
\$\endgroup\$
4
  • 12
    \$\begingroup\$ 1 is not a prime though...? \$\endgroup\$
    – Seggan
    Commented 10 hours ago
  • 9
    \$\begingroup\$ If there are no primes why is difference list not empty? \$\endgroup\$ Commented 9 hours ago
  • 9
    \$\begingroup\$ Please clarify if returning [0] instead of an empty list is a strict requirement (but be aware that this will probably be frowned upon). \$\endgroup\$
    – Arnauld
    Commented 5 hours ago
  • \$\begingroup\$ Was this asked in the sandbox first? \$\endgroup\$ Commented 1 hour ago

5 Answers 5

2
\$\begingroup\$

APL(NARS), 31 chars

{⍬≡m←⍵/⍨{⍵=1:1⋄0π⍵}¨⍵:⍬⋄⌽2-/⌽m}

Input one list of numbers (a range should be a list of numbers), output one list of numbers, that can be zilde ⍬ (that seems to be the void list of number), if that list of output is void.

test:

  f←{⍬≡m←⍵/⍨{⍵=1:1⋄0π⍵}¨⍵:⍬⋄⌽2-/⌽m}
  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│
└~─┘
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 12 7 bytes

ŸʒÒg!}¥

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ "Probably can be improved." It indeed can, since Ýʒ›≠} is basically builtin Ÿ. :) And I like your Òg2‹. I personally had (the equal bytes alternative) pyΘ~. But your approach does open up another golf by replacing 2‹ with !. In total (7 bytes): ŸʒÒg!}¥ - Try it online. \$\endgroup\$ Commented 6 hours ago
1
\$\begingroup\$

Vyxal, 9 bytes

ṡ'æn1=∨;¯

Try it Online!

+5 bytes because 1 is considered a prime in this challenge

Explained

ṡ'æn1=∨;¯­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁣​‎⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌­
ṡ          # ‎⁡Range [n, m]
 'æn1=∨;   # ‎⁢Keep only primes OR the number 1
        ¯  # ‎⁣and get the differences
💎

Created with the help of Luminespire.

\$\endgroup\$
1
\$\begingroup\$

Vyxal 3, 7 bytes

↯ʎ℗n”}δ

Vyxal It Online!

↯ʎ℗n”}δ­⁡​‎‎⁡⁠⁢‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏⁠‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌­
 ʎ   }   # ‎⁡filter...
↯        # ‎⁢inclusive range implicit inputs by...
  ℗      # ‎⁣is n prime?
   n     # ‎⁤push n
    ”    # ‎⁢⁡consume n. if it is 1, push it back, otherwise do nothing.
      δ  # ‎⁢⁢deltas of the resulting range
💎

Created with the help of Luminespire.

<script type="vyxal3">
↯ʎ℗n”}δ
</script>
<script>
    args=[["12","1"],["31","21"],["100","80"],["88","84"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>

\$\endgroup\$
1
\$\begingroup\$

Jelly, 6 bytes

Assuming, like most others, that the [0] result for no, or indeed only one, prime/1 in the range, shown in the examples section, is a mistake, and the code should instead produce an empty list in such cases. You could append »"0 for +3 bytes to force the behaviour, but there may be golfier ways.

f1;æRI

A dyadic Link that accepts the lower bound on the left and the upper bound on the right and yields the differences between consecutive primes, or 1 within the inclusive range.

Try it online!

How?

f1;æRI - Link: positive integer, LowerBound; positive integer, UpperBound
f1     - {[LowerBound]} filter keep 1 -> [] if LowerBound > 1 else [1]
   æR  - {LowerBound} inclusive prime range {UpperBound}
  ;    - {[] or [1]} concatenate {primes}
     I - forward differences
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.