问题描述
给定两个数字N1和N2 。
- 找出N1和N2之间的质数,然后
- 根据在步骤1中找到的质数列表,使所有可能的数字唯一组合成为可能。
- 从这个新列表中,再次找到所有素数。
- 从第二个生成的列表中找到最小的A和最大的B编号,并对该列表进行计数。
- 将最小和最大数分别作为生成斐波那契数列的第一个和第二个数,直到计数(第二个列表中的素数)。
- 打印斐波那契数列的最后一个数字作为输出
约束条件
2 <= N1,N2 <= 100
N2 – N1> = 35
例子:
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
方法:想法是使用Eratosthenes筛子检查在O(1)时间内特定数字是否为质数。因此,对从N1到N2的所有数字进行迭代,并将该范围内的所有质数存储在数组中,然后使用嵌套循环查找质数的所有唯一可能组合。最后,从所有组合中找到素数,然后是这些素数的最小值和最大值。使用最小和最大素数,我们可以生成斐波那契数列,以计算斐波那契数列的最后一项(所有组合中的素数数)。
下面是上述方法的实现:
C++
// C++ implementation to compute the
// combination of every possible
// prime numbers of the range
#include
using namespace std;
long long maxN = 1e5;
// Seive of Eratosthenes
void seive(vector& 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 primes(maxN, true);
// 1 in not prime
primes[1] = false;
// generate all prime in range
// using siieve of eratosthenes
seive(primes);
vector filteredPrimes;
vector comb;
set 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;
}
13158006689