Você está na página 1de 4

//Joshua Major

#include<iostream>
#include<vector>
//
Description of program
/*
This program takes a list of numbers, throw it in a number vector. I used another vector to check if the
sum of each set of 2 numbers are prime,
if they are, they belong in a candidate vector. In the input, the 2 numbers are the boundary values.
Based on the 5 functions I have,
the program will use backtracking check to see if there is a prime sequence using every number
between the first input value and the second. If there is, print
it out, if not, say that "no prim sequence exists".
*/
//
Description of data stucture used
/*
I used lists/vectors to implement my program. I didn't use arrays/matrices because I'm not that familiar
with them. Using a graph
data structure would be too complicated because graph can have more than one nodes from another
node. This program just involves
lists of numbers from one number to another.
*/
//
Evaluation
/*
I technically did this alone, even though I got help from the teacher. I believe I did a good job
considering I get the right output.
*/
using namespace std;
bool IsPrime(int number);
bool backtrack(vector<int> numbers, vector<int> candidates,int number);
void print(vector<int>numbers);
bool find(vector<int>numbers,int value);
bool legal(vector<int> numbers, vector<int> candidates,int nIndex);
int main()
{

int number1,number2;
vector<int> numbers;
vector<int>candidates;
bool found = true;
cin>>number1>>number2;
while (number1!=0 && number2 !=0)
{
for(int i = number1; i <= number2; i++)
numbers.push_back(i);
found = backtrack(numbers,candidates,0);
if(!found)
cout << "No Prime Sequence Exists" << endl;
numbers.clear();
candidates.clear();
cin>>number1>>number2;
}
system("pause");
return 0;
}
bool IsPrime(int number)
{
/*
pre-post: There has to be atleast 1 number to check
post-condition: number is evaluated as either a prime number or not
*/
int i;
for (i = 2; i < number; i++)
{
if (number % i == 0)
return false;
}
return true;
}
bool backtrack(vector<int> numbers, vector<int> candidates, int i)
{
/*
pre-condition: There is more than 1 number to check, otherwise it can't backtrack
post-condition: The array of numbers are checked from the vectors, and determines whether it's a
legal check, throw
the values in the candidate vector
*/
if(i >= numbers.size())
{

print(candidates);
return true;
}
else
{
for(int j = 0; j < numbers.size(); j++)
{
if (legal(numbers, candidates, j))
{
candidates.push_back(numbers[j]);
if(!backtrack(numbers,candidates,i+1))
candidates.pop_back();
else
{
return true;
}
}
}
return false;
}
}
void print(vector<int>numbers)
{
/*
post-condition: prints the numbers vector
*/
for(int i = 0; i < numbers.size(); i++)
cout << numbers[i] << " ";
cout << endl;
}
bool find(vector<int>candidates,int value)
{
/*
pre-condition: There's a number to find
post-condition: Locates the number in candidates vector, if the number is found, it's true
*/
for (int i = 0; i < candidates.size(); i++)
{
if(candidates[i] == value)
return true;
}
return false;
}
bool legal(vector<int> numbers, vector<int> candidates,int nIndex)
{
/*
pre-condition: There's numbers to check
post-condition: Tests the lagality of the 2 numbers based on if there are actually numbers in the

candidates and if the number's


already found
*/
if(candidates.empty())
return true;
else if(find(candidates,numbers[nIndex]))
return false;
else
return IsPrime(candidates.back() + numbers[nIndex]);
}

Você também pode gostar