Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range | C # program to implement the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int [ ] prime = new int [ 100001 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < 100001 ; i ++ ) prime [ i ] = 1 ; int p = 2 ; while ( p * p <= 100000 ) { if ( prime [ p ] == 1 ) { for ( int i = p * p ; i < 100001 ; i += p ) prime [ i ] = 0 ; } p += 1 ; } } static int join ( int a , int b ) { int mul = 1 ; int bb = b ; while ( b != 0 ) { mul *= 10 ; b /= 10 ; } a *= mul ; a += bb ; return a ; } static void fibonacciOfPrime ( int n1 , int n2 ) { SieveOfEratosthenes ( ) ; List < int > initial = new List < int > ( ) ; for ( int i = n1 ; i <= n2 ; i ++ ) if ( prime [ i ] == 1 ) initial . Add ( i ) ; List < int > now = new List < int > ( ) ; for ( int i = 0 ; i < initial . Count ; i ++ ) { for ( int j = 0 ; j < initial . Count ; j ++ ) { int a = initial [ i ] ; int b = initial [ j ] ; if ( a != b ) { int C = join ( a , b ) ; now . Add ( C ) ; } } } List < int > current = new List < int > ( ) ; for ( int i = 0 ; i < now . Count ; i ++ ) if ( prime [ now [ i ] ] == 1 ) current . Add ( now [ i ] ) ; int [ ] cnt = new int [ 1000009 ] ; for ( int i = 0 ; i < 1000001 ; i ++ ) cnt [ i ] = 0 ; List < int > current_set = new List < int > ( ) ; for ( int i = 0 ; i < current . Count ; i ++ ) { cnt [ current [ i ] ] ++ ; if ( cnt [ current [ i ] ] == 1 ) current_set . Add ( current [ i ] ) ; } long first = 1000000000 ; for ( int i = 0 ; i < current_set . Count ; i ++ ) first = Math . Min ( first , current_set [ i ] ) ; long second = 0 ; for ( int i = 0 ; i < current_set . Count ; i ++ ) second = Math . Max ( second , current_set [ i ] ) ; int count = current_set . Count - 1 ; long curr = 1 ; long c = 0 ; while ( curr < count ) { c = first + second ; first = second ; second = c ; curr += 1 ; } Console . WriteLine ( c ) ; } static void Main ( ) { int x = 2 ; int y = 40 ; fibonacciOfPrime ( x , y ) ; } }