-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab6.tar
More file actions
146 lines (113 loc) · 10 KB
/
Copy pathlab6.tar
File metadata and controls
146 lines (113 loc) · 10 KB
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
primes.cpp 0000600 0065137 0016273 00000002172 14323704552 012635 0 ustar lsmit248 lsmit248 // Lab 6: Is it prime?
/* Laura Smith 10/18/22 9:30 pm Primes Lab part 1
This part of the lab lists out primes in lines of 20 up to the inputed
max value. I used booleans and loops to do this and learned more about
both. This was the beginning of the lab and was the first step to later
solving other parts.
*/
#include <iostream>
using namespace std;
int main() {
int max; //max
bool is_prime = true; //bool for prime/notprime
cin >> max; //only takes in one max
int num = 2;
int count = 0; //to count how many are printed per line
while (num <= max){
if (num != 0 && num != 1){
for (int i = 2; i <= num/2; i++){ //goes through num/2 to test if it's prime or not
if (num % i == 0){
is_prime = false;
break;
}
is_prime = true;
}
}
if (is_prime){ //only prints out the primes
cout << num << ", ";
count ++;
if (count % 20 == 0){ // to keep it at 20 per line
cout << endl;
}
}
num++; //iteration
}
return 0;
} primes2.cpp 0000600 0065137 0016273 00000003452 14323704556 012725 0 ustar lsmit248 lsmit248 // Lab 6: Is it prime?
/* Laura Smith 10/18/22 9:30 pm Primes Lab part 2
This part of the lab stores the primes in a vector and then uses find to
determine if the number is prime based on if it was added to the vector or not.
This part of the lab taught me to use vector iterators and the stl function
find as well as improving my vector abilities.
*/
#include <iostream>
#include <vector> //vectors duh
#include <bits/stdc++.h> //for the iteration and find
#include <cmath> //used sqrt
using namespace std;
int main(int argc, char *argv[]) {
int max = 0; //user's inputted max
bool is_prime = true; //bool of is or isn't prime
vector <int> primes;
vector <int>::iterator it; //vector and iterator for primes
int num = 2;
int oldMax = max; //initializing max and old max to 0
while (cin >> max){
if (oldMax < max){ //only runs if there's more to add
while (num <= max){
if (num != 0 && num != 1){// to start at 2 to save time
for (int i = 2; i <= sqrt(num); i++){//goes through sqrt num to check if it's prime
if (num % i == 0){
is_prime = false;
break;
}
is_prime = true;
}
}
if (is_prime){//when it's prime it's added to the vector with pushback
// add to vector
primes.push_back(num);
}
num++; //iterate the number
}
}
oldMax = max; //to keep track of what the old max is
//find function which uses and iterator to look for the max
it = find (primes.begin(), primes.end(), max);
if (it != primes.end())
cout << "prime" << endl;
else
cout << "not prime" << endl;
}
return 0;
}