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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int  long long int NEW_LINE int prime [ 100001 ] ; 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 ; } } 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 ; } void fibonacciOfPrime ( int n1 , int n2 ) { SieveOfEratosthenes ( ) ; vector < int > initial ; for ( int i = n1 ; i <= n2 ; i ++ ) if ( prime [ i ] ) initial . push_back ( i ) ; vector < int > now ; for ( auto a : initial ) for ( auto b : initial ) if ( a != b ) { int c = join ( a , b ) ; now . push_back ( c ) ; } vector < int > current ; for ( auto x : now ) if ( prime [ x ] ) current . push_back ( x ) ; set < int > current_set ; for ( auto i : current ) current_set . insert ( i ) ; int first = * min_element ( current_set . begin ( ) , current_set . end ( ) ) ; int second = * max_element ( current_set . begin ( ) , current_set . end ( ) ) ; int count = current_set . size ( ) - 1 ; int curr = 1 ; int c ; while ( curr < count ) { c = first + second ; first = second ; second = c ; curr += 1 ; } cout << ( c ) << endl ; } int32_t main ( ) { int x = 2 ; int y = 40 ; fibonacciOfPrime ( x , y ) ; }