8
\$\begingroup\$

This question already has an answer here:

Bertrand's postulate states that there is always at least 1 prime number between n and 2n for all n greater than 1.

Challenge

Your task is to take a positive integer n greater than 1 and find all of the primes between n and 2n (exclusive).

Any default I/O method can be used. Whoever writes the shortest code (in bytes) wins!

Test cases

n    2n     primes

2    4      3
7    14     11, 13
13   26     17, 19, 23
18   36     19, 23, 29, 31
21   42     23, 29, 31, 37, 41
\$\endgroup\$

marked as duplicate by Mr. Xcoder code-golf Jan 8 '18 at 20:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    \$\begingroup\$ What if n = 1 for which there are no prime in (excluded) range (1, 2)? \$\endgroup\$ – user202729 Jan 8 '18 at 13:53
  • 4
    \$\begingroup\$ I edited your challenge to make it a bit more readable and clear. If you disagree with the changes, feel free to edit back. \$\endgroup\$ – Mr. Xcoder Jan 8 '18 at 13:57
  • 5
    \$\begingroup\$ @RosLuP: I doubt that anyone else sees a problem with this addition since it's about Bertand's postulate which talks about N > 1. I really don't get why you're trying to invalidate several answers just to make this a more cumbersome challenge that's not about said postulate. \$\endgroup\$ – ბიმო Jan 8 '18 at 16:02
  • 1
    \$\begingroup\$ Closely related, perhaps duplicate. Also closely related. \$\endgroup\$ – Mr. Xcoder Jan 8 '18 at 18:10
  • 3
    \$\begingroup\$ @RosLuP Every answer I can understand on this question, just makes a list and applies a primality test to the elements. This seems to be a trivial modification, in which case it is a duplicate. \$\endgroup\$ – Sriotchilism O'Zaic Jan 8 '18 at 20:10

20 Answers 20

3
\$\begingroup\$

Jelly, 4 bytes

‘æRḤ

Try it online!

waiting for OP's answer in comment...

(doesn't work for n=1)

\$\endgroup\$
  • \$\begingroup\$ The challenge states "for all n greater than 1". \$\endgroup\$ – Shaggy Jan 8 '18 at 17:06
  • \$\begingroup\$ @Shaggy Thanks! (for future readers, the condition was added after this answer was posted) \$\endgroup\$ – user202729 Jan 8 '18 at 17:17
3
\$\begingroup\$

Pari/GP, 20 bytes

n->primes([n+1,2*n])

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Haskell, 45 bytes

-2 bytes thanks to BMO.

f n=[i|i<-[n+1..2*n],all((>0).mod i)[2..i-1]]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Japt, 6 bytes

ôU Åfj

Explanation:

 ôU Åfj
U        // Implicit input                     7
 ôU      // Inclusive range [Input...Input+U]  [7,8,9,10,11,12,13,14]
    Å    // Remove the first item              [8,9,10,11,12,13,14]
     fj  // Filter primes                      [11,13]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Was just about to suggest a similar 8-byte solution when you edited. Great work! \$\endgroup\$ – ETHproductions Jan 8 '18 at 14:51
  • \$\begingroup\$ Ah, so that's what N.ô() does! \$\endgroup\$ – Shaggy Jan 8 '18 at 15:36
2
\$\begingroup\$

Octave, 27 bytes

@(n)(k=n+1:2*n)(isprime(k))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Octave, 24 bytes

@(n)(k=primes(2*n))(k>n)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

PL/SQL, 270 bytes

declare
 n number:=7;
 p number;
 v number:=0;
begin
 for i in (n+1)..(n*2)-1 loop
  p:=0;
  for j in 2..trunc(sqrt(n*2)) loop
   if mod(i,j)=0 then p := 1;
    exit;
   end if;
  end loop;
  if p =0 then 
   dbms_output.put_line(i);
   v:=v+1;
  end if;
 end loop;
end;
\$\endgroup\$
  • 3
    \$\begingroup\$ Any solution is worth posting, especially if you want to post something! Every answer needs to be a solution. \$\endgroup\$ – totallyhuman Jan 8 '18 at 16:16
  • 4
    \$\begingroup\$ Welcome to PPCG! I tried to format your code, but I'm not sure if it came out as you intended. Is indentation meaningful in PL/SQL? Could it be removed to save bytes? \$\endgroup\$ – Dennis Jan 8 '18 at 16:55
  • \$\begingroup\$ you can pull out the indentation, for what it's worth. It doesn't affect how SQL runs. \$\endgroup\$ – phroureo Jan 8 '18 at 20:00
  • \$\begingroup\$ @Dennis As user phroureo pointed out above, this solution is suboptimal (i.e. not a serious contender). Why did you undelete it? Also {@}David (I doubt the OP will read this anyway, because their account is unregistered) It's not important whether the solution is short or not, the problem is whether it's the shortest in the language or not. \$\endgroup\$ – user202729 Jan 9 '18 at 11:36
  • \$\begingroup\$ @user202729 Because the post was deleted for being a stub, and it's not a stub anymore. I don't have the first clue about PL/SQL (or even SQL), so I can't judge whether this is a serious contender or even a valid answer, but I figured it deserved at least a chance to get peer-reviewed. \$\endgroup\$ – Dennis Jan 9 '18 at 13:48
1
\$\begingroup\$

05AB1E, 5 bytes

·ÅPʒ‹

Try it online!

How?

·ÅPʒ‹ || Full program.
      ||
·     || Double the input.
 ÅP   || Lists all the prime lower than or equal to ^.
   ʒ‹ || Filter-keep those which are greater than the input.
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 57 bytes

param($n)(2*$n-1)..++$n|?{'1'*$_-match'^(?!(..+)\1+$)..'}

Try it online!

Takes input $n, constructs a range from 2*$n-1 to ++$n (i.e., excluding the endpoints), and feeds that range into a Where-Object cmdlet. The clause is the regex pattern match against unary numbers. Thus, those numbers where that clause is true (i.e., they are prime) are filtered out of the range and left on the pipeline. Output is implicit.

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

JavaScript (ES6), 63 62 bytes

Saved 1 bytes thanks to @Arnauld

f=(n,q=n,x)=>n?q%x?f(n,q,x-1):f(n-1,q+1,q).concat(x<2?q:[]):[]
\$\endgroup\$
1
\$\begingroup\$

APL NARS, 45 bytes, 26 chars

{⍵≤1:⍬⋄(0πt)/t←(⍵+1)..2×⍵}

Perhaps better the range in Axiom: When a>b than a..b is the void set. Test

  g←{⍵≤1:⍬⋄(0πt)/t←(⍵+1)..2×⍵}
  g 7
11 13 
  g 9
11 13 17 
  g 0

  g 1

  g 2
3 
  g 6.3
DOMAIN ERROR
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 27 bytes

{grep &is-prime,$_^..^2*$_}

Try it

Expanded:

{  # bare block lambda with implicit parameter 「$_」

  grep         # find all values

    &is-prime, # that are prime

               # from the following

      $_
      ^..^     # create a Range that excludes both endpoints
      2 * $_
}
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 39 bytes

⎕CY'dfns'⋄
{1↓(10pco(⍵+0⍵))/(-⍵)↑⍳⍵+⍵-1}

Try it online!

The characters B← are not counted towards the byte count because they're unnecessary and are added to TIO so it's easier to call the function. Also, ⍎'⎕CY''dfns''⋄⍬' is the equivalent to ⎕CY'dfns'⋄, but TIO doesn't accept the usual APL notation for that, so the extra characters are needed.

Thanks to @ErikTheOutgolfer for 1 byte.

How it works:

⎕CY'dfns'⋄                          ⍝ Imports every direct function. This is needed for the primes function, 'pco'.
{1↓(10pco(⍵+0⍵))/(-⍵)↑⍳⍵+⍵-1}      ⍝ Main function, prefix.
   (10pco      )                   ⍝ Calls the function 'pco' with the modifier 10, which lists every prime between the arguments:
         (⍵+0⍵)                    ⍝ Vector of arguments, ⍵ and 2⍵.
                                   ⍝ The function returns a boolean vector, with 1s for primes and 0s otherwise.
 1↓                                ⍝ Drops the first element of the vector (because Bertrands postulate excludes the left bound).
                /                  ⍝ Replicate right argument to match the left argument.
                      ⍳⍵+⍵-1       ⍝ Generate range from 1 to 2⍵-1 (to exclude the right bound)
                     ↑             ⍝ Take from the range
                 (-⍵)              ⍝ The last ⍵ elements.
\$\endgroup\$
  • \$\begingroup\$ You don't need the space in 0 ⍵. \$\endgroup\$ – Erik the Outgolfer Jan 8 '18 at 15:29
1
\$\begingroup\$

Python 3, 59 57 bytes

def f(n):k=m=1;exec('m%k*k>n!=print(k);m*=k*k;k+=1;'*2*n)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 5 bytes

xŸ¦ʒp

Try it online!

Explanation

xŸ¦ʒp  Full Program
x      double top of stack
 Ÿ     inclusive range
  ¦    remove first element (pop a: push a[1:])
   ʒ   filter to keep
    p  prime elements

-1 byte thanks to Mr. Xcoder

\$\endgroup\$
0
\$\begingroup\$

C, 72 bytes

i,k;f(n){for(k=n*2;++n<k;i<k&&printf("%d ",n))for(i=1;++i<n;)i=n%i?i:k;}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java 8, 95 bytes

q->{for(int n=q+1,i,x;n<2*q;n++){for(x=n,i=2;i<x;x=x%i++<1?0:x);if(x>1)System.out.println(n);}}

Try it online.

Explanation:

  • q->{...}: Method with integer parameter and no return-type
    • for(int n=q+1;n<2*q;n++){...}: Loop from q+1 to 2*q (exclusive)
      • for(x=n,i=2;i<x;...);: Loop from 2 to n (exclusive)
        • x=x%i++<1?0:x: If x is divisible by anything, change it to 0, otherwise leave it the same.
      • if(x>1)...: This means that if x is not 0 at the end of this inner loop, it is a prime (not divisible by anything but itself or 1).
        • System.out.println(n);: So print this prime n
\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica) 28 bytes

This is fairly straightforward: within the range of (n+1,2n), select numbers that are primes.

Range[#+1,2#]~Select~PrimeQ&

Example

Range[#+1,2#]~Select~PrimeQ&[7]

(* {11, 13} *)
\$\endgroup\$
  • \$\begingroup\$ You can save some bytes on the range: Select[#+Range@#,PrimeQ]& \$\endgroup\$ – Martin Ender Jan 8 '18 at 15:07
0
\$\begingroup\$

Husk, 6 bytes

fṗtS…D

Try it online!

Explanation

fṗtS…D -- example input: 7
   S…  -- range from itself (inclusive) to itself..
     D -- | .. doubled
       -- : [7,8,9,10,11,12,13,14]
  t    -- tail: [8,9,10,11,12,13,14]
fṗ     -- filter primes: [11,13]

Alternative, 6 bytes

fṗ§…→D

Try it online!

Explanation

fṗ§…→D -- example input: 7
  §…   -- range from ..
     D -- | .. itself incremented to
       -- | .. itself doubled
       -- : [8,9,10,11,12,13,14]
fṗ     -- filter primes: [11,13]
\$\endgroup\$
0
\$\begingroup\$

Julia 0.4, 17 bytes

Prime related functions wered moved to a package in Julia 0.5, so this would require using Primes in newer versions, and also TIO doesn't have that package installed.

n->primes(n+1,2n)

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.