473,698 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prime class

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<uns igned 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[i]);
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p[i] * p[i]; i++)
if (p.front() % p[i] == 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

Dec 8 '06 #1
6 1641

za******@gmail. com wrote:
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<uns igned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}
// use an init list
Prime() : p(2, 3) { } // (size, initial value)
>
~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p[i]);
Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_erro r
}
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p[i] * p[i]; i++)
if (p.front() % p[i] == 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.
Why not use a functor?
>
@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
Dec 8 '06 #2

Salt_Peter skrev:
za******@gmail. com wrote:
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<uns igned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

// use an init list
Prime() : p(2, 3) { } // (size, initial value)

~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p[i]);

Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_erro r
}
Even though i have played with C++ for quite a while, im not very
skilled. There is allways other way to do it. What you wrote above is
very confusing to me, most of the code i have never seens it before,
however if it does the job better im very interested in knowing how it
actually works.
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p[i] * p[i]; i++)
if (p.front() % p[i] == 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.

Why not use a functor?
Again, all those weird expressions ;-) what is a funtor, do you mean
moving the functions outside the class? if thats the case, i simple
like it this way and the end result should be the same, if im not
mistaken.

@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
Dec 8 '06 #3

Salt_Peter skrev:
za******@gmail. com wrote:
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<uns igned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

// use an init list
Prime() : p(2, 3) { } // (size, initial value)

~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p[i]);

Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_erro r
}
Even though i have played with C++ for quite a while, im not very
skilled. There is allways other way to do it. What you wrote above is
very confusing to me, most of the code i have never seens it before,
however if it does the job better im very interested in knowing how it
actually works.
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p[i] * p[i]; i++)
if (p.front() % p[i] == 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.

Why not use a functor?
Again, all those weird expressions ;-) what is a funtor, do you mean
moving the functions outside the class? if thats the case, i simple
like it this way and the end result should be the same, if im not
mistaken.

@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
Dec 8 '06 #4
za******@gmail. com writes:
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<uns igned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
why return(0)?
how about
return 0;
the same for below.
Dec 8 '06 #5

za******@gmail. com wrote:
Salt_Peter skrev:
za******@gmail. com wrote:
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<uns igned int>p;
>
public:
>
Prime() {
p.push_back(3);
p.push_back(3);
}
// use an init list
Prime() : p(2, 3) { } // (size, initial value)
>
~Prime() {}
>
unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p[i]);
Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_erro r
}
Even though i have played with C++ for quite a while, im not very
skilled. There is allways other way to do it. What you wrote above is
very confusing to me, most of the code i have never seens it before,
however if it does the job better im very interested in knowing how it
actually works.
What i suggested is nothing new, C++ constructor's supports an
initialization list. Why allocate a member and then initialize it to
some value when you can do both simultaneously.

class A {
int a;
public:
A() : a(0) { } // sets private member
A(int n) : a(n) { }
void foo() const { }
};

std::size_t is a default size type returned by sizeof() operator -
which is implementation defined. In the above example, foo() is
declared const because i chose not to let foo() access the object's
internals. That way foo() does not get passed the hidden 'this'
parameter. So in your case, declaring get() as constant guarentees that
get() will not attempt to modify your private parts - even by accident.
This is an important concept since it improves the sfaety of the class
and prevents side-effects.

To understand vector's at() function, uncomment the call to at() below
since the vector does not have an 11th element.

#include <iostream>
#include <ostream>
#include <vector>
#include <stdexcept>

int main()
{
try {
std::vector< unsigned vu(10, 99);
for(std::size_t i = 0; i < 10; ++i)
{
std::cout << "vu[" << i << "] = ";
std::cout << vu.at(i) << std::endl;
}
// std::cout << vu.at(10); // will throw std::range_erro r
} catch (const std::exception& r_e)
{
std::cerr << "Error: " << r_e.what();
std::cerr << std::endl;
}
return 0;
}

/*
vu[0] = 99
vu[1] = 99
vu[2] = 99
vu[3] = 99
vu[4] = 99
vu[5] = 99
vu[6] = 99
vu[7] = 99
vu[8] = 99
vu[9] = 99
*/
}
>
unsigned int next() {
>
p.front() += 2;
>
for (int i = 1; p.front() >= p[i] * p[i]; i++)
if (p.front() % p[i] == 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.
Why not use a functor?
Again, all those weird expressions ;-) what is a funtor, do you mean
moving the functions outside the class? if thats the case, i simple
like it this way and the end result should be the same, if im not
mistaken.
Don't worry about functors for now. You might consider them later,
though. Something like returning the set of even, odd or prime numbers
are perfect candidates for functors.
>
@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
Dec 8 '06 #6

Salt_Peter skrev:
za******@gmail. com wrote:
Salt_Peter skrev:
za******@gmail. com wrote:
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<uns igned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}
>
// use an init list
Prime() : p(2, 3) { } // (size, initial value)
>

~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p[i]);
>
Since the std::vector already has a bounds_checking accessor at():
>
unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_erro r
}
>
Even though i have played with C++ for quite a while, im not very
skilled. There is allways other way to do it. What you wrote above is
very confusing to me, most of the code i have never seens it before,
however if it does the job better im very interested in knowing how it
actually works.

What i suggested is nothing new, C++ constructor's supports an
initialization list. Why allocate a member and then initialize it to
some value when you can do both simultaneously.

class A {
int a;
public:
A() : a(0) { } // sets private member
A(int n) : a(n) { }
void foo() const { }
};

std::size_t is a default size type returned by sizeof() operator -
which is implementation defined. In the above example, foo() is
declared const because i chose not to let foo() access the object's
internals. That way foo() does not get passed the hidden 'this'
parameter. So in your case, declaring get() as constant guarentees that
get() will not attempt to modify your private parts - even by accident.
This is an important concept since it improves the sfaety of the class
and prevents side-effects.

To understand vector's at() function, uncomment the call to at() below
since the vector does not have an 11th element.

#include <iostream>
#include <ostream>
#include <vector>
#include <stdexcept>

int main()
{
try {
std::vector< unsigned vu(10, 99);
for(std::size_t i = 0; i < 10; ++i)
{
std::cout << "vu[" << i << "] = ";
std::cout << vu.at(i) << std::endl;
}
// std::cout << vu.at(10); // will throw std::range_erro r
} catch (const std::exception& r_e)
{
std::cerr << "Error: " << r_e.what();
std::cerr << std::endl;
}
return 0;
}

/*
vu[0] = 99
vu[1] = 99
vu[2] = 99
vu[3] = 99
vu[4] = 99
vu[5] = 99
vu[6] = 99
vu[7] = 99
vu[8] = 99
vu[9] = 99
*/
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p[i] * p[i]; i++)
if (p.front() % p[i] == 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.
>
Why not use a functor?
>
Again, all those weird expressions ;-) what is a funtor, do you mean
moving the functions outside the class? if thats the case, i simple
like it this way and the end result should be the same, if im not
mistaken.

Don't worry about functors for now. You might consider them later,
though. Something like returning the set of even, odd or prime numbers
are perfect candidates for functors.
As you must have used quite alot of time explaining this i can only
oppologize that i understand close to nothing of it. Maybe its because
english is not my first language or maybe im just plain stupid, either
way i wont ask you to explain again.
Thank you for your time.

@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
Dec 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
8390
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N, but a prime number tester would also be nice. I'm dealing with numbers in the 10^6-10^8 range so it would have to fairly efficient Dag
0
2397
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has been made by A & A Group. Muhammad Ali: Roll # 1462 Class A-2 , B.Sc.(Hons.) in C.S.
20
6814
by: Tuvas | last post by:
I have made and recently posted a libary I made to do Modular Arithmetic and Prime numbers on my website at http://www.geocities.com/brp13/Python/index.html . I am currently in a crypotology class, and am working on building a RSA public key cryptology system for a class project. I am building the librarys just to get the experience to do so. However, I would ask if any of you know of any gaping security holes that can easily be seen from...
3
3212
by: hirunisha | last post by:
Hi Im a beginner in C++. I tried to do these but couldnt. Pls hekp me to do these. thanks. 1. Design and implement a class called PrimeChecker. An instance of this class will be able to check if a number between 2-10000 is prime. The class stores the consecutive primes 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 in an array and uses these to check if the given number is prime. The...
2
2605
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not shown) import math import sys print "Welcome to the Coordinate Geometry Calculator!"
3
5081
by: rcarwise | last post by:
Iam having trouble getting started on this program and wanted to know if I could get help on writing a method and loops to get started. this is the program that I have to do: Write a program that reads an integer number n > 1, and writes the input number’s prime factors. The following are a few input/output examples: Input Output 12 2,2,3
3
1649
by: jzakiya | last post by:
This is to announce the release of my paper "Ultimate Prime Sieve -- Sieve of Zakiiya (SoZ)" in which I show and explain the development of a class of Number Theory Sieves to generate prime numbers. I used Ruby 1.9.0-1 as my development environment on a P4 2.8 Ghz laptop. You can get the pdf of my paper and Ruby and Python source from here: http://www.4shared.com/dir/7467736/97bd7b71/sharing.html Below is a sample of one of the...
3
3029
by: ace2606 | last post by:
Hi I am a beginner in C++ and would like someone to give me an idea or push me in the right direction of how to solve this question , It is related to classes Design and implement a class called PrimeChecker to check if a number is prime. The class stores an array of consecutive primes. It uses this array to check if a given number is prime. To simplyfy the excercise the array will store at most 1000 prime numbers. The class has the...
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.