#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
//判断数字是否为质数
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
//生成所有小于1993的质数
vector<int>GeneratePrimes(int limit) {
vector<int>Primes;
for (int i = 2; i <= limit; ++i) {
if (isPrime(i)) {
Primes.push_back(i);
}
}
return Primes;
}
//生成各质数之间的差值
vector<int>GenerateDifferences(const vector<int>& Primes) {
vector<int> Differences;
for (size_t i = 0; i < Primes.size() - 1; ++i) {
Differences.push_back(Primes[i+1] - Primes[i]);
}
return Differences;
}
//判断和为1898的情况有几种
int countContinuousSums(const vector<int>& Differences, int target) {
int count = 0;
for (size_t i = 0; i < Differences.size(); ++i) {
int sum = 0;
for (size_t j = i; j < Differences.size(); ++j) {
sum += Differences[j];
if (sum == target) {
count++;
}
else if (sum > target) {
break;
}
}
}
return count;
}
int main()
{
int limit = 1993;
int target = 1898;
vector<int>Primes = GeneratePrimes(limit);
vector<int>Differences = GenerateDifferences(Primes);
int result = countContinuousSums(Differences,target);
cout << "Number of ways to sum to " << target << " with consecutive differences: " << result << endl;
return 0;
}