Nested prime function

Let p n p_n denote the n th n^\text{th} prime number , then the following equations are true.

p 1 = 2 p p 1 = 3 p p p 1 = 5 \begin{aligned} p_1 &= & 2 \\ p_{p_1} &= & 3 \\ p_{p_{p_1}} &= & 5 \\ \end{aligned}

Find the value of N N such that p p p p p 1 number of p ’s = N = 648391 \large \underbrace{p_{p_{p_{p_{p_{\cdot_{\cdot_{\cdot_1}}}}}}}}_{\text{number of }p\text{'s} =N} = 648391 .


The answer is 10.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

First Last
Mar 2, 2017
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool prime ;
int primes[1000000] ; int count = 3 ; //start for primes array
primes[0] = 2 ; primes[1] = 3 ; primes[2] = 5 ; primes[3] = 7 ;

for(int i = 11 ; ; i += 2){ //create possible primes
     prime = true ;
     for(int a = 3 ; a <= sqrt(i) ; a += 2) if(i%a == 0){ //check relevant factors
             prime = false ;
             break ;
    }
    if(prime){
         count ++ ;
         primes[count] = i ;
         if(i == 648391) break ; 
    }  
}
int value = 1 ;  
count = 0 ;
while(value != 648391){ //loop through cases of Nth prime
     value = primes[value-1] ;
     count ++ ;
}
cout << count ;

As well, the Nth prime could be generated on demand, overall being a better/smarter method, but this works fine for such a problem.

Yayyyyyy!!!!

Pi Han Goh - 4 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...