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
20 Answers 20
-
\$\begingroup\$ The challenge states "for all n greater than 1". \$\endgroup\$Shaggy– Shaggy2018年01月08日 17:06:37 +00:00Commented Jan 8, 2018 at 17:06
-
\$\begingroup\$ @Shaggy Thanks! (for future readers, the condition was added after this answer was posted) \$\endgroup\$user202729– user2027292018年01月08日 17:17:03 +00:00Commented Jan 8, 2018 at 17:17
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]
-
\$\begingroup\$ Was just about to suggest a similar 8-byte solution when you edited. Great work! \$\endgroup\$ETHproductions– ETHproductions2018年01月08日 14:51:21 +00:00Commented Jan 8, 2018 at 14:51
-
\$\begingroup\$ Ah, so that's what
N.ô()does! \$\endgroup\$Shaggy– Shaggy2018年01月08日 15:36:05 +00:00Commented Jan 8, 2018 at 15:36
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;
-
3\$\begingroup\$ Any solution is worth posting, especially if you want to post something! Every answer needs to be a solution. \$\endgroup\$totallyhuman– totallyhuman2018年01月08日 16:16:58 +00:00Commented Jan 8, 2018 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– Dennis2018年01月08日 16:55:24 +00:00Commented Jan 8, 2018 at 16:55
-
\$\begingroup\$ you can pull out the indentation, for what it's worth. It doesn't affect how SQL runs. \$\endgroup\$phroureo– phroureo2018年01月08日 20:00:34 +00:00Commented Jan 8, 2018 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– user2027292018年01月09日 11:36:21 +00:00Commented Jan 9, 2018 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– Dennis2018年01月09日 13:48:49 +00:00Commented Jan 9, 2018 at 13:48
05AB1E, 5 bytes
·ÅPʒ‹
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.
PowerShell, 57 bytes
param($n)(2*$n-1)..++$n|?{'1'*$_-match'^(?!(..+)1円+$)..'}
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.
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:[]):[]
APL NARS, 45 bytes, 26 chars
{⍵≤1:⍬⋄(0πt)/t←(⍵+1)×ばつ⍵}
Perhaps better the range in Axiom: When a>b than a..b is the void set. Test
g←{⍵≤1:⍬⋄(0πt)/t←(⍵+1)×ばつ⍵}
g 7
11 13
g 9
11 13 17
g 0
g 1
g 2
3
g 6.3
DOMAIN ERROR
Perl 6, 27 bytes
{grep &is-prime,$_^..^2*$_}
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 * $_
}
APL (Dyalog Unicode), 39 bytes
⎕CY'dfns'⋄
{1↓(10pco(⍵+0⍵))/(-⍵)↑⍳⍵+⍵-1}
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.
-
\$\begingroup\$ You don't need the space in
0 ⍵. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2018年01月08日 15:29:30 +00:00Commented Jan 8, 2018 at 15:29
05AB1E, 5 bytes
xŸ¦ʒp
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
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} *)
-
\$\begingroup\$ You can save some bytes on the range:
Select[#+Range@#,PrimeQ]&\$\endgroup\$Martin Ender– Martin Ender2018年01月08日 15:07:47 +00:00Commented Jan 8, 2018 at 15:07
Husk, 6 bytes
fṗtS...D
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
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]
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)
Java 8, (削除) 95 (削除ここまで) 92 bytes
q->{for(int n=q,i,x;++n<2*q;){for(x=n,i=2;i<x;x=x%i++<1?0:x);if(x>1)System.out.println(n);}}
-3 bytes thanks to @ceilingcat.
Explanation:
q->{ // Method with integer parameter and no return-type
for(int n=q,i,x;++n<2*q;){
// Loop `n` in the range (q,2q):
for(x=n, // Set `x` to `n`
i=2;i<x // Loop `i` in the range [2,n):
; // After every iteration:
x=x%i++<1? // If `x` is divisible by `i`:
0 // Set `x` to 0
: // Else:
x); // Leave it unchanged
if(x>1) // If `x` is larger than 1 at the end of the inner loop
// (which means it's a prime):
System.out.println(n);}}
// Print this prime `n`
Explore related questions
See similar questions with these tags.
n = 1for which there are no prime in (excluded) range (1, 2)? \$\endgroup\$