Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range | Stores at each index if it 's a prime or not  ; Sieve of Eratosthenes to generate all possible primes ; If p is a prime ; Set all multiples of p as non - prime ; Function to generate the required Fibonacci Series ; Stores all primes between n1 and n2 ; Generate all primes between n1 and n2 ; Stores all concatenations of each pair of primes ; Generate all concatenations of each pair of primes ; Stores the primes out of the numbers generated above ; Store the unique primes ; Find the minimum ; Find the minimum ; Find N ; Print the N - th term of the Fibonacci Series ; Driver Code
let prime = new Array ( 100001 ) ; function SieveOfEratosthenes ( ) { for ( let i = 0 ; i < 100001 ; i ++ ) prime [ i ] = 1 ; let p = 2 ; while ( p * p <= 100000 ) { if ( prime [ p ] == 1 ) { for ( let i = p * p ; i < 100001 ; i += p ) prime [ i ] = 0 ; } p += 1 ; } } function join ( a , b ) { let mul = 1 ; let bb = b ; while ( b != 0 ) { mul *= 10 ; b = parseInt ( b / 10 , 10 ) ; } a *= mul ; a += bb ; return a ; } function fibonacciOfPrime ( n1 , n2 ) { SieveOfEratosthenes ( ) ; let initial = [ ] ; for ( let i = n1 ; i <= n2 ; i ++ ) if ( prime [ i ] == 1 ) initial . push ( i ) ; let now = [ ] ; for ( let i = 0 ; i < initial . length ; i ++ ) { for ( let j = 0 ; j < initial . length ; j ++ ) { let a = initial [ i ] ; let b = initial [ j ] ; if ( a != b ) { let c = join ( a , b ) ; now . push ( c ) ; } } } let current = [ ] ; for ( let i = 0 ; i < now . length ; i ++ ) if ( prime [ now [ i ] ] == 1 ) current . push ( now [ i ] ) ; let cnt = new Array ( 1000009 ) ; for ( let i = 0 ; i < 1000001 ; i ++ ) cnt [ i ] = 0 ; let current_set = [ ] ; for ( let i = 0 ; i < current . length ; i ++ ) { cnt [ current [ i ] ] ++ ; if ( cnt [ current [ i ] ] == 1 ) current_set . push ( current [ i ] ) ; } let first = 1000000000 ; for ( let i = 0 ; i < current_set . length ; i ++ ) first = Math . min ( first , current_set [ i ] ) ; let second = 0 ; for ( let i = 0 ; i < current_set . length ; i ++ ) second = Math . max ( second , current_set [ i ] ) ; let count = current_set . length - 1 ; let curr = 1 ; let c = 0 ; while ( curr < count ) { c = first + second ; first = second ; second = c ; curr += 1 ; } document . write ( c ) ; } let x = 2 ; let y = 40 ; fibonacciOfPrime ( x , y ) ;