Prime Fibonnaci | TCS Mockvita 2020

Problem Description

Given two numbers N1 and N2.

  1. Find prime numbers between N1 and N2, then
  2. Make all possible unique combinations of numbers from the prime numbers list you found in step 1.
  3. From this new list, again find all prime numbers.
  4. Find smallest A and largest B number from the 2nd generated list, also count of this list.
  5. Consider smallest and largest number as the 1st and 2nd number to generate Fibonacci series respectively till the count (Number of primes in the 2nd list).
  6. Print the last number of a Fibonacci series as an output

Constraints

2 <= N1, N2 <= 100

N2 – N1 >= 35

Examples:



Input:  N1 = 2, N2 = 40
Output: 13158006689
Explanation:
First prime list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

Combination of all the primes = [23, 25, 27, 211, 213, 217, 219, 223, 229, 231, 32, 35, 37, 311, 313, 319, 323, 329, 331, 337, 52, 53, 57, 511, 513, 517, 519, 523, 529, 531, 537, 72, 73, 75, 711, 713, 717, 719, 723, 729, 731, 737, 112, 113, 115, 117, 1113, 1117, 1119, 1123, 1129, 1131, 1137, 132, 133, 135, 137, 1311, 1317, 1319, 1323, 1329, 1331, 1337, 172, 173, 175, 177, 1711, 1713, 1719, 1723, 1729, 1731, 1737, 192, 193, 195, 197, 1911, 1913, 1917, 1923, 1929, 1931, 1937, 232, 233, 235, 237, 2311, 2313, 2317, 2319, 2329, 2331, 2337, 292, 293, 295, 297, 2911, 2913, 2917, 2919, 2923, 2931, 2937, 312, 315, 317, 3111, 3113, 3117, 3119, 3123, 3129, 3137, 372, 373, 375, 377, 3711, 3713, 3717, 3719, 3723, 3729, 3731]

Second prime list=[193, 3137, 197, 2311, 3719, 73, 137, 331, 523, 1931, 719, 337, 211, 23, 1117, 223, 1123, 229, 37, 293, 2917, 1319, 1129, 233, 173, 3119, 113, 53, 373, 311, 313, 1913, 1723, 317]

smallest (A) = 23

largest (B) = 3719
Therefore, the last number of a Fibonacci series i.e. 34th Fibonacci number in the series that has 23 and 3719 as the first 2 numbers is 13158006689

Input: N1 = 30, N2 = 70
Output: 2027041
Explanation:

First prime list = [31, 37, 41, 43, 47, 53, 59, 61, 67]

Second prime list generated form combination of 1st prime list = [3137, 5953, 5347, 6761, 3761, 4337, 6737, 6131, 3767, 4759, 4153, 3167, 4159, 6143]
Smallest prime in 2nd list=3137
Largest prime in 2nd list=6761
Therefore, the last number of a Fibonacci series i.e. 14th Fibonacci number in the series that has 3137 and 6761 as the first 2 numbers is 2027041

 

Approach: The idea is to use Sieve of Eratosthenes to check that a particular number is a prime number or not in O(1) time. Therefore, Iterate over all the numbers from N1 to N2 and store all the prime numbers in that range in an array and then using Nested Loop find all unique possible combinations of the prime numbers. Finally, find the prime numbers from all the combination and then the minimum and the maximum of those prime numbers. Using the minimum and the maximum prime numbers we can generate the Fibonacci series to compute the last term (Number of prime numbers in all the combinations) of the Fibonacci series.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to compute the 
// combination of every possible 
// prime numbers of the range
  
#include <bits/stdc++.h>
using namespace std;
  
long long maxN = 1e5;
  
// Seive of Eratosthenes
void seive(vector<bool>& primes)
{
    for (long long num = 2; 
         num * num < maxN; num++) {
        if (primes[num]) {
            for (long long i = num * num;
                       i < maxN; i += num)
                primes[i] = false;
        }
    }
}
  
// Function to find the Nth term of
// of the Fibonacci series
long long solve(long long N1, 
                long long N2)
{
    vector<bool> primes(maxN, true);
    // 1 in not prime
    primes[1] = false;
    // generate all prime in range 
    // using siieve of eratosthenes
    seive(primes);
  
    vector<string> filteredPrimes;
    vector<long long> comb;
    set<long long> lst;
  
    // filter required primes and
    // put them into filteredPrimes
    // as strings
    for (long long i = N1; i <= N2; i++)
        if (primes[i])
            filteredPrimes.push_back(
                          to_string(i));
  
    // make all possible combinations
    for (long long i = 0; 
         i < (long long)(filteredPrimes.size()); 
                                        i++) {
        for (long long j = 0; 
             j < (long long)(filteredPrimes.size()); 
                                        j++) {
            if (i == j)
                continue;
  
            string tmp = filteredPrimes[i] + 
                         filteredPrimes[j];
            comb.push_back(stoi(tmp));
        }
    }
  
    // Filter only prime numbers
    // for generated combinations
    for (long long x : comb)
        if (primes[x])
            lst.insert(x);
  
    auto it = lst.end();
    it--;
    
    // take smallest and largest element
    long long a = *(lst.begin()), b = *it, c;
  
    // Now find last element
    // of fibonacci series
    for (long long i = 3; 
         i <= (long long)(lst.size()); 
                                 i++) {
        c = a + b;
        a = b;
        b = c;
    }
  
    return c;
}
  
// Driver Code
int main()
{
    long long N1 = 2, N2 = 40;
  
    cout << solve(N1, N2);
  
    return 0;
}

chevron_right


Output

13158006689

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.