-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPrimos.cpp
More file actions
72 lines (61 loc) · 1.55 KB
/
Primos.cpp
File metadata and controls
72 lines (61 loc) · 1.55 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
#include "Primos.h"
#include <math.h>
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
Primos::Primos(int inicio, unsigned int tamanho, Interceptador *interceptador) : Calculo(inicio, tamanho, interceptador) {
this->resultados.reserve(tamanho);
}
void Primos::calcula() {
int ini = this->inicio;
if (ini == 0 || ini == 1) {
ini = 3;
this->resultados.push_back(2);
}
while(this->resultados.size() < this->tamanho) {
bool isPrimo = true;
for(int i = 2; i <= sqrt(ini); i++) {
if(ini%i == 0) {
isPrimo = false;
break;
}
}
if(isPrimo) this->resultados.push_back(ini);
ini++;
}
}
unsigned int Primos::numeroResultados() {
return this->resultados.size();
}
int Primos::resultado(unsigned int indice) {
int rtn = 0;
if(indice + 1 <= this->resultados.size()) {
rtn = this->resultados.at(indice);
}
return this->interceptador->intercepta(rtn);
}
string Primos::toString(char sep){
string str;
string a;
unsigned int i = 0;
char s[2] = {sep, 0};
for(vector<int>::iterator it = this->resultados.begin(); it != this->resultados.end(); it++){
a = to_string(*it);
str.append(a);
if(i < (this->resultados.size() - 1)){
str.append(s);
}
i++;
}
return str;
}
void Primos::limpaCalculo() {
this->resultados.clear();
}
string Primos::nome() const{
return "Primos";
}
Primos::~Primos() {
this->limpaCalculo();
}