Question #40009

how to write a program that puts the integer from 1 to N into each circle separately, in such a way that the sum of the numbers in any two adjacent circle is a prime number??

input:
the input to the program is a positive, non-zero even integer N. Assume that 0<N<=20.

Output:
the out put of the program must be the sequence of numbers in a ring, starting from circle marked with the value 1, and proceeding in a clockwise direction.

>>>
sample input and output:

if the value for N is 6
the produce sequence can be: 1 4 3 2 5 6

if the value for N is 8
the produced sequence is: 1 2 3 8 5 6 7 4
1

Expert's answer

2014-03-17T14:47:10-0400

Answer on Question#40009- Programming, C++

1. How to write a program that puts the integer from 1 to N into each circle separately, in such a way that the sum of the numbers in any two adjacent circle is a prime number??

input:

the input to the program is a positive, non-zero even integer N. Assume that 0<=20.

Output:

the output of the program must be the sequence of numbers in a ring, starting from circle marked with the value 1, and proceeding in a clockwise direction.

<:>

sample input and output:

if the value for N is 6

the produce sequence can be: 1 4 3 2 5 6

if the value for N is 8

the produced sequence is: 1 2 3 8 5 6 7 4

Solution.

#include <iostream>
#include <vector>
using namespace std;
bool is_simple(int num);
int main(int argc, char* argv[])
{
    int num = 0, buf = 0;
    vector<int> stack;
    cout << "Enter a number from 1 to 20: "; cin >> num;
    if (num >= 1 && num <= 20)
    {
        cout << "The produced sequence is: ";
        for (int i = 1; i <= num; i++)
        {
            stack.push_back(i);
        }
        for (int i = 1; i <= stack.size(); i++)
        {
            if (is_simple(stack[i - 1] + buf))
            {
                cout << stack[i - 1] << " ";
                buf = stack[i - 1];
                for (int j = i; j < stack.size(); j++)
                {
                    stack[j - 1] = stack[j];
                }
                stack.pop_back();
                i = 0;
            }
        }
    }
    else
    {
        cout << endl << "You have entered number does not match the range!";
    }
    cin.get(); cin.get();
    return 0;
}
bool is_simple(int num)
{
    if (num == 1)
    {
        return true;
    }
    for (long int i = 2; i < num; i++)
    {
        if (num % i == 0)
        {
            return false;
        }
    }
    return true;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS