check if a number is prime from an array

The problem is in the main function where the for loop starts. I need to figure out if a number from my arraySizeBase is prime and if it is then my string array for that base is true and if its not then its false.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void Base (int listBase[], int numberOfRows, int startingPoint, int increment);
void Square (int listBase[], int listSquare[], int numberOfRows);
void SquareRoot (int listBase[], double listSquareRoot[], int numberOfRows);
void Cube (int listBase[], int listCube[], int numberOfRows);
void CubeRoot (int listBase[], double listCubeRoot[], int numberOfRows);
void Even (int listBase[], string listEven[], int numberOfRows);
void Prime (int listBase[], string listPrime[], int numberOfRows);


void PrintAll (int listBase[], int listSquare[], double listSquareRoot[], int listCube[], double listCubeRoot[], string listEven[], int numberOfRows);

int main() {
    int arraySizeBase[25];
    int arraySizeSquare[25];
    double arraySizeSquareRoot[25];
    int arraySizeCube[25];
    double arraySizeCubeRoot[25];
    string arraySizeEven[25];
    string arraySizePrime[25];
    int size;
    cout << "Enter the number of rows in your table between 1 and 25: ";
    cin >> size;
    cout << endl;
    while (size < 1 || size > 25)
    {
        cout << "Invalid input" << endl;
        cout << "Please enter a valid Number of rows in your table between 1 and 25: ";
        cin >> size;
    }

    int startPoint;
    cout << "Enter a number to start the table with between - 1000 and 1000: ";
    cin >> startPoint;
    cout << endl;
    while (startPoint < -1000 || startPoint > 1000)
    {
        cout << "Invalid input" << endl;
        cout << "Please enter a valid Number to start the table with between -1000 and 1000: ";
        cin >> startPoint;
    }

    int increment;
    cout << "Enter a number to increment table between 1 and 20: ";
    cin >> increment;
    cout << endl;
    while (increment < 1 || increment > 20)
    {
        cout << "Invalid input" << endl;
        cout << "Please enter a valid Number to increment table between 1 and 20: ";
        cin >> increment;
    }

    Base(arraySizeBase, size, startPoint, increment);
    //Square(arraySizeBase, arraySizeSquare, size);
    //SquareRoot(arraySizeBase, arraySizeSquareRoot, size);
    //Cube(arraySizeBase, arraySizeCube, size);
    //CubeRoot(arraySizeBase, arraySizeCubeRoot, size);
    //Even(arraySizeBase, arraySizeEven, size);

    //PrintAll(arraySizeBase, arraySizeSquare, arraySizeSquareRoot, arraySizeCube, arraySizeCubeRoot, arraySizeEven, size);



    for (int index = 0; index <= size ; index++)
    {
        if (arraySizeBase[index] < 2) {arraySizePrime[index] = "False";}
        else { if (arraySizeBase[index] == 2 || arraySizeBase[index] == 3) { arraySizePrime[index] = "True";}}

        for (int i = 2; i <=arraySizeBase[index]/2; i++){

            if (arraySizeBase[index] % i == 0)
            {arraySizePrime[index] = "False";}
            else { arraySizePrime[index] = "True";}
        }

    }





    for (int j = 0; j < size; j ++)
    {
        cout << arraySizeBase [j] << " " << arraySizePrime[j] <<endl;}




    return 0;
}


void Base (int listBase[], int numberOfRows, int startingPoint, int increment)
{
    listBase [0] = startingPoint;

    for (int index = 1; index < numberOfRows; index++)
    {
        int newValue = startingPoint += increment;
        listBase[index] = newValue;
    }
}

void Square (int listBase[], int listSquare[], int numberOfRows)
{
    for (int index = 0; index < numberOfRows ; index++)
    {
        int newValue = listBase[index] * listBase[index];
        listSquare[index] = newValue;
    }
}

void SquareRoot (int listBase[], double listSquareRoot[], int numberOfRows)
{
    for (int index = 0; index < numberOfRows ; index++)
    {
        double newValue = sqrt(listBase[index]);
        if (listBase[index] < 0)
        {

        }
        else
        {
            listSquareRoot[index] = sqrt(listBase[index]);
        }
    }
}

void Cube (int listBase[], int listCube[], int numberOfRows)
{
    for (int index = 0; index < numberOfRows ; index++)
    {
        int newValue = listBase[index] * listBase[index] * listBase[index];
        listCube[index] = newValue;
    }
}

void CubeRoot (int listBase[], double listCubeRoot[], int numberOfRows)
{
    for (int index = 0; index < numberOfRows ; index++)
    {
        double newValue = pow(listBase[index], 1/3.);
        listCubeRoot[index] = newValue;
    }
}

void Even (int listBase[], string listEven[], int numberOfRows)
{
    for (int index = 0; index < numberOfRows ; index++)
    {
        int newValue = listBase[index] % 2;
        if (newValue != 1 && newValue !=-1) {listEven[index] = "True";}
        else {listEven[index] = "False";}
    }
}


void PrintAll (int listBase[], int listSquare[], double listSquareRoot[], int listCube[], double listCubeRoot[], string listEven[], int numberOfRows)
{
    for (int j = 0; j < numberOfRows; j ++)
    {
        cout << listBase[j] << " " << listSquare[j] << " " << listSquareRoot[j] << " " << listCube[j] << " "<< listCubeRoot[j] << " " << listEven[j] << endl;
    }
}

one simple algorithm for this is:
a number is prime if gcd(N, sqrt(N)! ) is 1.

GCD is built into C++ algorithms or is a 2 line program using %s.
Factorial may run into trouble if your numbers get too large, in which case you may have to use brute force instead or use a large int class.

you can substitute factorial with *product of all primes up to sqrt(N)* which grows slower but still fast.

there are several other ways to do it for 64 bit and smaller ints; even brute force works.
a loop of n = 3 to sqrt N n+=2, if N%n == 0 not prime will do it (check 2 as well up front).

if I read you code right, the numbers are "around 1000", which is sqrt = ~30 which is doable with factorial method. You also seem to allow negative numbers; I think these methods may need you to look at the abs to use them.
Last edited on
Topic archived. No new replies allowed.