I am trying to write a program which checks if every even number in an interval (a,b) can be portrayed as the sum of two prime numbers.
In the end the even number with the fewest representations as sum of prime numbers should be in the output.
There are no errors shown, but the execution file will only work until you type in the numbers a and b for the interval.
I think i did a mistake by using vector there. Can anybody help?
#include <iostream>
#include <vector>
usingnamespace std;
bool is_prime(int n) //function that proves if a number is prime
{
if (n < 2) {
returnfalse;
}
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
returnfalse;
}
}
returntrue;
}
void print_vector(int min, int min_count, std:: vector<int>& min_comb){
cout << "the even number with the fewest representations is:" <<min<< "\n"
<<"The representations are: "<< endl;
for (int i=0; i<min_count; ++i){
cout << " "<<min_comb[i]<<"+"<<min-min_comb[i]<<"\n";
}
}
int main ( )
{
int a, b; //interval
int k,j, p; //variables
int min; //number of minimal representat.
int min_count=b; //
int count; //Number of represent. in total
vector<int> comb(b/4);
//this vector with size b/4 saves one primesummand of each representation
vector<int> min_comb(b/4);
//this vector saves the primesummands of the number with the fewest representations
cout << " 1. Type in start number : ";
cin >> a;
cout << " Type in end number : ";
cin >> b;
for (int i=a; i <=b; i=i+1){
count=0; \\number of represntations
if (i %2==0) { //only consider even numbers
for (k=2; k<= i/2; ++k){
if ( is_prime(k)){
if ( is_prime(i-k) ){
comb[count]=k;
count ++;
}
}
}
}
if (count < min_count){
min = i;
min_count = count;
for (count-=1; count >= 0; --count){
min_comb[count] = comb[count];
}
}
}
print_vector(min, min_count, min_comb);
}