473,387 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

using nested for loop, workin with displaying prime numbers

Hey. Im trying to make program that basically displays all the prime
numbers. I need to use bool and for but im not quite sure if i have this
right. So far i have this bunch of a mess but can anyone point me in the
right direction? Thanks!
# include <iostream>
# include <cmath>
using namespace std;

int i;
//int sqrt;
//int j;
int x;
int j;
int limit;
bool primeNumber(int);

int main() {

// cout << "Limit: ";
// cin >x;
// if ((x % 2 == 0) & (x % x == 0))
// {
// return true;
// }
// else
// {
// return false;
// return 0;
//}
//
//bool primeNumber; {
// if ((x % 2 == 0) & (x % x == 0))
// {
// return true;
// }
// else
// {
// return false;
// }
//}

for (i = 2; i <= x; i++)
cout << i;
{
for (j = 2; j <= sqrt(i); i++)
cout << sqrt(100.0) << endl;
cout << sqrt(static_cast<double>(100)) << endl; //static cast conversts
from one type to another
}
return 0;
}

Nov 7 '06 #1
3 3818
"triplejump24" <lo********@yahoo.comwrote:
Hey. Im trying to make program that basically displays all the prime
numbers. I need to use bool and for but im not quite sure if i have this
right. So far i have this bunch of a mess but can anyone point me in the
right direction?
Here's a pointer for you. Start with the code below. When you run it, it
will *not* print "working so far". Insert code where it says "insert
code here" until you can get "working so far" to print (It should be
quite easy :-) After you get that part done, un-comment the second
assert and modify the code until 'working so far" prints again, then the
next comment, &c.
#include <iostream>
#include <cassert>
// other includes as necessary

bool is_prime( int number )
{
bool result = false;
// insert code here
return result;
}

int main() {
assert( is_prime( 2 ) );
//assert( is_prime( 3 ) );
//assert( ! is_prime( 4 ) );
//assert( is_prime( 5 ) );
cout << "Working so far!";
}

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #2
Hey. Im trying to make program that basically displays all the prime
numbers. I need to use bool and for but im not quite sure if i have this
right. So far i have this bunch of a mess but can anyone point me in the
right direction? Thanks!
First ask yourself what is a primenumber?
"An integer p is called a prime number if the only positive integers
that divide p are 1 and p itself."

So you need to check your would-be primenumber against all other
integers. Well you do not need to check againts all other integers.
Any integer larger than p will not divide p!
So we need to check all integers smaller than p (excluding 0 and 1).
This is something you should definitely use a loop for.
You can use a "while()" loop, but i would prefer a "for()" loop.
something like for(int n= 2; n<p;n++) in side the loop you can then
check if n divides p. If it does, p is not a prime.
If the loop completes without n dividing p, p is a prime.

If you put all this into a function called IsPrime(int p) which can
return a bool. ( true if the argument was a primenumber, false if
not)

if you now want test a number you can call

if(IsPrime(number_to_test))
{
// This was a prime number
}

You can now use a loop to test all the numbers you would like...
(hint "for loop") or you can write each test explicitly
if(Isprime(2))
:
:
if(Isprime(34))

I know there is a lot of room for improvement in my is prime
function. But start by focussing on getting the function to work
correctly first, then you can always optimise the code if needed.
One optimization is only to test integers smaller than or equal to
the squareroot of p. another ( and more difficult) is to only test a
against all primenumbers smaller than the square root of p, provided
you know ALL of them!!

Two things I think you should consider is what should happen is you
call IsPrime(0) and IsPrime(1). You wil probably have to modify the
IsPrime function to handle this in a sensible way.

A few comments on the code:
using namespace std;
See http://www.parashift.com/c++-faq-lit...standards.html
section 27.5 for a discussion.

You need to use more comments in your code!
If you have a varible, you should make a comment why its there, and
what is is used for. The same applies to functions and files.
make it a good habbit to write many comments, and most importantly
keep the comments in sync with the code!
cout << sqrt(static_cast<double>(100)) << endl;
Using a cast operator is something that should make you think "Is
this really right?". Casting is often a consecuence of a flawed
design. In some cases there are needed, but avoid them as much as
possible.

I hope this get you closer to your program.

Bo Møller

--
Bo Møller

Hobby-Programmer
Nov 7 '06 #3
Don

triplejump24 wrote:
Hey. Im trying to make program that basically displays all the prime
numbers.
#include <stdio.h>

/* This program implements a blindingly fast O(n^n) algorithm
to find prime numbers, using an elegant recursive method.
As required it uses bool and for. */
bool _(int n, int m, int d)
{
bool r = m != n;
for(int i=0; d && (i<n); i++)
r &= _(n,(m<=n)?i*m:0,d-1)|!_(i,1,i);
return r;
}

/*------------------------------------------
Print primes up to the requested value
--------------------------------------------*/
int main(int argc, char* argv[])
{
for(int n = 2; n; n++)
printf("%d is%s prime\n",n, _(n,1,n)?"" : " not");
return 0;
}

Nov 10 '06 #4

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

Similar topics

8
by: | last post by:
I have to write a program that displays all of the perfect numbers less than 1000. I know I need 2 for loops and at least one if statement. What I need to know is, is there any method in Java to...
4
by: Don Low | last post by:
Hi, I'm studying the python tutorial at python.org. In introducing the concept of *break* and *continue* Statements, and *else* Clauses on Loops, there's an example that goes as follows: ...
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
5
by: Candace | last post by:
I have to write a program to find all Pythagorean triples for a right triangle. I know that the squares of two sides of the triangle must equal the square of the third (longest) side. However, I...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
4
by: cnixuser | last post by:
Hello, I am attempting to create a prime number detector that will identify all of the prime numbers within a specified range of numbers. The problem is, for some reason my program is not detecting...
9
by: notahipee | last post by:
Would someone be able to tell me why this isn't working. The nested for loops seem correctly coded to me. I would appreciate any input. #include <iostream.h> #include <math.h> int main () {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.