Is it a super-prime?

22

1

Background

A super-prime is a prime number whose index in the list of all primes is also prime. The sequence looks like this:

3, 5, 11, 17, 31, 41, 59, 67, 83, 109, 127, 157, 179, 191, ...

This is sequence A006450 in the OEIS.

Challenge

Given a positive integer, determine whether it is a super-prime.

Test Cases

2: false
3: true
4: false
5: true
7: false
11: true
13: false
17: true
709: true
851: false
991: true

Scoring

This is , so the shortest answer in each language wins.

musicman523

Posted 2017-07-07T21:20:55.917

Reputation: 4 472

6What is the index of 2? Is it 1 or 0? – Dennis – 2017-07-07T21:44:34.567

1@Dennis the sequence is 1-indexed; the index of 2 is 1. – musicman523 – 2017-07-07T22:03:12.113

2First thought after reading what a super-prime is: What would you call super-super-primes? Or super^3-primes? What is bigger, the number of atoms in the universe or the 11th super^11-prime? You, dear internet person, are stealing another few hours of my hours of my prime time! – J_F_B_M – 2017-07-08T19:28:52.790

@J_F_B_M Make a challenge based on it! :D – musicman523 – 2017-07-09T06:03:59.717

It's a bird! It's a plane! It's Super-Prime! – Mark – 2017-07-09T09:23:08.210

1@J_F_B_M 11 is a super-prime who's index in the super-prime list is also a super-prime (3), so the 11'th super-prime is a super-super-super-prime – Skidsdev – 2017-07-10T08:33:48.987

435748987787 happens to be the 11th super^11-prime, for anyone interested. – J_F_B_M – 2017-07-10T13:19:41.457

Answers

21

Jelly, 5 bytes

ÆRÆNċ

Try it online!

How it works

ÆRÆNċ  Main link. Argument: n

ÆR     Prime range; yield the array of all primes up to n.
  ÆN   N-th prime; for each p in the result, yield the p-th prime.
    ċ  Count the occurrences of n.

Dennis

Posted 2017-07-07T21:20:55.917

Reputation: 196 637

8Gosh darn it, you win again... – ETHproductions – 2017-07-07T21:50:04.070

3He always does... – Gryphon – 2017-07-08T04:20:14.657

@ETHproductions Well, solution is pretty obvious...it's just the ninja here. – Erik the Outgolfer – 2017-07-08T13:52:00.457

14

Mathematica, 26 23 bytes

Thanks to user202729 for saving 3 bytes.

PrimeQ/@(#&&PrimePi@#)&

This makes use of the fact that Mathematica leaves most nonsensical expressions unevaluated (in this case, the logical And of two numbers) and Map can be applied to any expression, not just lists. So we compute the And of the input and its prime index, which just remains like that, and then we Map the primality test over this expression which turns the two operands of the And into booleans, such that the And can then be evaluated.

Martin Ender

Posted 2017-07-07T21:20:55.917

Reputation: 184 808

123 bytes: PrimeQ/@(#&&PrimePi@#)&. – user202729 – 2017-07-08T03:36:02.120

@user202729 Nice, thank you. :) – Martin Ender – 2017-07-08T06:37:20.380

10

Jelly, 6 bytes

ÆRi³ÆP

Try it online!

Uses the same technique as my Japt answer: Generate the primes up to n, get the index of n in that list, and check that for primality. If n itself is not prime, the index is 0, which is also not prime, so 0 is returned anyway.

ETHproductions

Posted 2017-07-07T21:20:55.917

Reputation: 47 880

9

Japt, 13 11 bytes

õ fj bU Ä j

Test it online!

Explanation

This is actually very straight-forward, unlike my original submission:

 õ fj bU Ä  j    
Uõ fj bU +1 j    Ungolfed
                 Implicit: U = input integer
Uõ               Generate the range [1..U].
   fj            Take only the items that are prime.
      bU         Take the (0-indexed) index of U in this list (-1 if it doesn't exist).
         +1 j    Add 1 and check for primality.
                 This is true iff U is at a prime index in the infinite list of primes.
                 Implicit: output result of last expression

ETHproductions

Posted 2017-07-07T21:20:55.917

Reputation: 47 880

4

Python 3, 104 97 93 bytes

p=lambda m:(m>1)*all(m%x for x in range(2,m))
f=lambda n:p(n)*p(len([*filter(p,range(n+1))]))

Returns 0/1, at most 4 bytes longer if it has to be True/False.

Try it online!

C McAvoy

Posted 2017-07-07T21:20:55.917

Reputation: 229

1

0/1 is fine. Nice answer! Since you don't ever use the value of f, you can reformat your code like this and exclude it from the byte count.

– musicman523 – 2017-07-07T21:57:11.137

@musicman523 Thanks for the tip! – C McAvoy – 2017-07-09T22:22:49.943

3

Jelly, 7 bytes

ÆCÆPaÆP

Try it online!

ÆC counts the number of primes less than or equal to the input (so, if the input is the nth prime, it returns n). Then ÆP tests this index for primality. Finally, a does a logical AND between this result and ÆP (primality test) of the original input.

Doorknob

Posted 2017-07-07T21:20:55.917

Reputation: 68 138

3

Haskell, 62 bytes

p x=2==sum[1|0<-mod x<$>[1..x]]
f x=p$sum[1|y<-[1..x],p y,p x]

Try it online! Usage: f 991 yields True.

Laikoni

Posted 2017-07-07T21:20:55.917

Reputation: 23 676

2

Pyth, 12 bytes

&P_QP_smP_dS

Try it online!

Explanation

&P_QP_smP_dS
                Implicit input
       mP_dS    Primality of all numbers from 1 to N
      s         Sum of terms (equal to number of primes ≤ N)
    P_          Are both that number
&P_Q            and N prime?

notjagan

Posted 2017-07-07T21:20:55.917

Reputation: 4 011

2

05AB1E, 6 bytes

ÝØ<Øså

Try it online!

Explanation

ÝØ<Øså
Ý      # Push range from 0 to input
 Ø     # Push nth prime number (vectorized over the array)
  <    # Decrement each element by one (vectorized)
   Ø   # Push nth prime number again
    s  # swap top items of stack (gets input)
     å # Is the input in the list?

Datboi

Posted 2017-07-07T21:20:55.917

Reputation: 1 213

2

Pyke, 8 bytes

sI~p>@hs

Try it here!

s        -  is_prime(input)
 I~p>@hs - if ^:
  ~p>    -    first_n_primes(input)
     @   -    ^.index(input)
      h  -   ^+1
       s -  is_prime(^)

Blue

Posted 2017-07-07T21:20:55.917

Reputation: 26 661

2

Perl 6, 46 bytes

{$/=&is-prime;all($_∈*,$/)([grep $/,1..$_])}

Try it online!

Sean

Posted 2017-07-07T21:20:55.917

Reputation: 4 136

1

Mathematica, 35 29 bytes

P=Prime;!P@P@Range@#~FreeQ~#&

-6 bytes from @MartinEnder

J42161217

Posted 2017-07-07T21:20:55.917

Reputation: 15 931

P@P@Range@# should save a bunch. – Martin Ender – 2017-07-07T21:55:09.273

1

QBIC, 33 bytes

~µ:||\_x0]{p=p-µq|~q=a|_xµp]q=q+1

Explanation

~   |   IF   ....  THEN (do nothing)
  :         the number 'a' (read from cmd line) 
 µ |        is Prime
\_x0        ELSE (non-primes) quit, printing 0
]           END IF
{           DO
            In this next bit, q is raised by 1 every loop, and tested for primality. 
            p keeps track of how may primes we've seen (but does so negatively)
    µq|     test q for primality (-1 if so, 0 if not)
p=p-        and subtract that result from p (at the start of QBIC: q = 1, p = 0)
~q=a|       IF q == a
_xµp        QUIT, and print the prime-test over p (note that -3 is as prime as 3 is)
]           END IF
q=q+1       Reaise q and run again.

steenbergh

Posted 2017-07-07T21:20:55.917

Reputation: 7 772

1

Haskell, 121 bytes

f=filter
p x=2==(length$f(\a->mod(x)a==0)[1..x])
s=map(\(_,x)->x)$f(\(x,_)->p x)$zip[1..]$f(p)[2..]
r x=x`elem`(take x s)

Sergii Martynenko Jr

Posted 2017-07-07T21:20:55.917

Reputation: 213

1(\(_,x)->x) is snd, (\(x,_)->p x) is (p.fst). Both fst and snd are in Prelude, so no need for imports. – Laikoni – 2017-07-07T23:52:27.470

Don't use backticks too often: r x=elem x$take x s. However, in this case you can go pointfree (introducing backticks again) and omit the function name: elem<*>(`take`s). – nimi – 2017-07-08T12:32:48.690

1

Positron, 148 bytes

x=#(input@@)a=function{p=1;k=$1==2;f=2;while(f<$1)do{p=p*$1%f;k=1;f=f+1};return p*k}r=1;i=2;while(i<x)do{if(a@i)then{r=r+1}i=i+1}print@((a@x*a@r)>0)

Try it online!

HyperNeutrino

Posted 2017-07-07T21:20:55.917

Reputation: 26 575

1

Pari/GP, 31 bytes

n->(p=isprime)(n)*p(primepi(n))

Try it online!

alephalpha

Posted 2017-07-07T21:20:55.917

Reputation: 23 988

1

Matlab, 36 34 bytes

Saved 2 bytes thanks to Tom Carpenter.

A very naive implementation using built-in functions:

isprime(x)&isprime(nzz(primes(x)))

Leander Moesinger

Posted 2017-07-07T21:20:55.917

Reputation: 300

1For Octave only you can also save a further byte with (p=@isprime)(x)&p(nnz(primes(x))) – Tom Carpenter – 2017-07-08T17:28:08.737

1

Python 2, 89 bytes

def a(n):
 r=[2];x=2
 while x<n:x+=1;r+=[x]*all(x%i for i in r)
 return{n,len(r)}<=set(r)

Try it online!

Constructs r, the list of primes <= n; if n is prime, then n is the len(r)'th prime. So n is a super prime iff n in r and len(r) in r.

Chas Brown

Posted 2017-07-07T21:20:55.917

Reputation: 8 959

1

Python 2, 79 bytes

n=input()
s={2};p=i=1
while i<n:
 p*=i;i+=1
 if p*p%i:s|={i}
print{n,len(s)}<=s

Try it online!

miles

Posted 2017-07-07T21:20:55.917

Reputation: 15 654

0

Julia 0.6, 61 bytes

return 1 if x is a super-prime, 0 otherwise.

without using an isprime-kind function.

x->a=[0,1];for i=3:x push!(a,0∉i%(2:i-1))end;a[sum(a)]&a[x]

Tanj

Posted 2017-07-07T21:20:55.917

Reputation: 199