Z
zacariaz
Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.
I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.
@code start:
#include <vector>
class Prime {
std::vector<unsigned int>p;
public:
Prime() {
p.push_back(3);
p.push_back(3);
}
~Prime() {}
unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p);
}
unsigned int next() {
p.front() += 2;
for (int i = 1; p.front() >= p * p; i++)
if (p.front() % p == 0) {
p.front() += 2;
i = 0;
}
p.push_back(p[0]);
return(p.back());
}
};
@code end!
For those who cant figure out how it work, here is a little piece of
code that displays the first ten primes.
@code start:
#include <iostream>
int main() {
Prime P;
for (int n = 0; n < 8; n++)
P.next();
for (int n = 0; P.get(n) != 0;n++)
std::cout << P.get(n) << std::endl;
std::cout << "Error, not enough primes!";
std::cin.get();
}
@code end!
The most important function is of course that which calculates the
primes: Prime::next() and it calculates one prime per execution. (is
that the right expression?)
Then there is the Prime::get() which enables you to get a specific
prime.
Other functions will come, but thats not important right now.
Well, i wont say much more at the time being, but i hope to hear some
advices, oppinions, etc.
Regards
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.
I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.
@code start:
#include <vector>
class Prime {
std::vector<unsigned int>p;
public:
Prime() {
p.push_back(3);
p.push_back(3);
}
~Prime() {}
unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p);
}
unsigned int next() {
p.front() += 2;
for (int i = 1; p.front() >= p * p; i++)
if (p.front() % p == 0) {
p.front() += 2;
i = 0;
}
p.push_back(p[0]);
return(p.back());
}
};
@code end!
For those who cant figure out how it work, here is a little piece of
code that displays the first ten primes.
@code start:
#include <iostream>
int main() {
Prime P;
for (int n = 0; n < 8; n++)
P.next();
for (int n = 0; P.get(n) != 0;n++)
std::cout << P.get(n) << std::endl;
std::cout << "Error, not enough primes!";
std::cin.get();
}
@code end!
The most important function is of course that which calculates the
primes: Prime::next() and it calculates one prime per execution. (is
that the right expression?)
Then there is the Prime::get() which enables you to get a specific
prime.
Other functions will come, but thats not important right now.
Well, i wont say much more at the time being, but i hope to hear some
advices, oppinions, etc.
Regards