Geee, I swear to God I had to just remove all the info, because it simply gave me tons of errors, it just doesn't work ... I'm helpless.
Main Topics
Browse All TopicsWrite a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number such that one and itself are the only numbers that evenly divide it ( e.g., 3, 5, 7, 11, 13, 17 ... ).
One way to solve this problemis to use a doubly nested loop. The outer loop can iterate from 3 to 100 while the inner loop checks to see if the counter value for the outer loop is prime. One way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n-1 evenly divide n, then n must be prime. ( Note that there are several easy ways to make this algorithm more efficient.)
Please someone help me, it's a take-home test project, and I am totally sleepy, I tried many ways to solve it but it didn't work. Anyone? Please? I need the whole project, because I have another one about liters and gallons but I'll try to do it my own. I'm so stuck. =[
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> it just doesn't work ... I'm helpless.
Can you still post what you have ? You must have gotten somewhere at least, especially since the assignment is very specific about what you have to do :
"The outer loop can iterate from 3 to 100 while the inner loop checks to see if the counter value for the
outer loop is prime. One way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers
evenly divides n, then n cannot be prime. If none of the values from 2 to n-1 evenly divide n, then n must be
prime."
Which part of this gives you trouble ?
Have you written programs in C++ before ?
I've only done a simple one and all of a sudden she gave us something that she didn't even bother to explain and just wants us to figure it out ourselves. =S
I'm not a genius or can predict things or have some super powers. That's why I need help, gosh. =[
I couldn't work it out at all. I dunno even know what is a nested loop ... gee. I'm not joking when I said I'm helpless.
Ok, in that case, I would start out by reading up on C++. Here are a few tutorials to get you up-to-date :
http://www.cplusplus.com/d
It's a recommended read !
Regarding your specific question :
>> I dunno even know what is a nested loop
A nested loop is one loop inside the body of another. For example two nested for loops :
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 20; ++j) {
// some code
}
}
So, the inner for loop (the one with j as index) will be executed 10 times. The total amount of times that "some code" will be executed is thus 10 * 20 = 200 times.
Does that make sense ?
Yeah, it does. But I kind of don't know a lot about that.
Thanks for the help really, though I don't think I'll be able to actually do my assignment, because it needs practice, and it's already night and oh well. It's not my fault that I do not understand, it's just she gives us things that she didn't even bother to explain ....
Thanks a lot anyway ...
I am working on it. I'll try my best.
However, it would be a really great favor if you know how to do it. You won't lose anything, and I don't really rely on others when I want to do my homeworks and stuff, that's not me, but this was urgent so I thought of asking. But still thanks if you don't want to do it, I don't blame you. =]
Take it one step at a time ... Do you know what prime numbers are ? If not, they're numbers that are divisible only by themselves and by 1.
If you're wondering about what should be in the for loops, read the assignment : it's very specific about that. Just give it a go, and write some code (one step at a time as I said earlier), and post it here ... that's the easiest way :)
Start with the skeleton of a C++ program. Check this tutorial for that :
http://www.cplusplus.com/d
I assume you have a compiler ready, and know how to use it ?
>> for ( int n = 3, n >= 100, n++)
A for loop consists of 3 parts :
1) the initialization : this is set at the beginning of the loop
2) the loop condition : as long as this is true, the loop will continue
3) the increment : this is done at the end of each iteration
and is structured like this :
for (<initialization>; <loop condition>; increment) {
// loop body
}
Note that a ; is used to separate the 3 parts (that's your first problem). Also note that the loop condition, is the condition that needs to be fulfilled as long as the loop runs. As soon as this condition becomes false, the loop ends. So it needs to be :
for ( int n = 3; n <= 100; n++)
I know what prime numbers are of course, but I can't do them in C++ programming, only math, lol.
Aaaah, okay, I know that I should print all the prime numbers from 3 to 100 in the first loop. But I just don't know how. I feel like crying. Pfft.
Oh and by the way, I know that I was supposed to put return 0; but I didn't because the program isn't over yet hehe. It's like I didn't even start.
>> Aaaah, okay, I know that I should print all the prime numbers from 3 to 100 in the first loop.
Indeed. Now, the loop goes from 3 to 100. If you put :
cout << n << " ";
inside the loop, the code will print out all numbers from 3 to 100 (try it to see what I mean).
We don't want to print all numbers - we want to print ONLY the prime numbers. So, for each number, we'll have to find out whether it's a prime or not. If it's a prime, we print it, if it's not, we don't print it.
How can we know whether a number is a prime ? That's nicely explained in the assignment. Take a look at it, and try to add the code for that.
>> We divide by 2 to see if it's prime or not?
A prime is a number that is divisible only by itself and by 1. If it's divisible by any other value, then it's not a prime. You need to check whether the number n is divisible by any value between 2 and n-1. As it says in the assignment :
"One way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers evenly divides n,
then n cannot be prime."
>> Umm the cout << n << " "; thing .. what do I put between " "
Nothing. " " is just a space. Try putting it inside the for loop we had earlier, and compile it. Then run it, and see what happens. Writing a program is constantly adding code, then running it to see whether the added code does what it's supposed to do.
>> do Ihave to list the whole numbers for 3 to 100?
Not for the assignment no. For the assignment, you only need to list the prime numbers between 3 and 100.
Regarding your problem with prime numbers, it's a typical homework problem and as such there are any number of tutorials and examples on the web! Try Googl'ing for "C++ Prime Numbers" and checking out a few of the links. At EE the experts cannot and will not provide you with full code solutions; however, Infinity08 is very good at maths related problems and is especially patient -- you are in good hands :)
>> I know that I was supposed to put return 0;
Actually, that's not strictly true, since (according to the standard) main() in C++ doesn't actually have to return anything (it just has to be defined to have a return type of int). It is; however, it is good practice to get into the habit of always returning a value form main. Usually, 0 is used to indicate success any other value conventionally means an error has ocurred.
"A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;"
>>>> for(int n = 3; n <=100 ; n++)
>>>> {
ok. that for loop repeats 98 times iterating n from 3 to 100 (both edges including).
To print the primes only you simply have to find out whether a 'n' divides by numbers lower than 'n'. Without optimizations that simply can be done by adding a nested loop (Infinity already explained what that is) which tries:
for (int i = 2; i < n; ++i)
{
That inner loop would run from 2 to n-1 where 'n' is the 'n' of the outer loop.
to test for prime you now have check whether 'n' can be divided by 'i' without rest. For that the modulo operator % is useful which computes the rest of an division, i. e. the term 'n % i' is the rest of 'n / i' and must not be 0 if n is a prime. Otherway round, in case 'n%i' is 0 you could break the inner loop with a 'non-prime' result, as the test has proven n isn't a prime.
if (/* put here the modulo term */ ... == 0)
{
isprime = false;
break;
}
The 'isprime' is a bool helper which must be set to true above the inner loop:
bool isprime = true;
for (int i = 2; i < n; ++i)
{
....
After the inner loop you have to decide whether to print 'n' as a prime or not. Fortunately, you only need to test on the 'isprime' already filled correctly.
if (isprime) // means the inner loop didn't find a reason to break
cout << /* output a statement that n is prime */ .... << endl;
If the program works, you should optimize it:
1. Run the outer loop with a step of 2 as no prime can be a multiple of 2.
for (.....; n += 2)
2. Run the inner loop starting with 3 (as the even n already were filtered)
Make a step of 2 as well cause n is an odd number and no odd number can be dividided without rest by an even number.
3. Make the upper boundary of the inner loop the sqare root of n.
Any integer i greater than the square root cannot be a divisor of n.
Regards, Alex
First, try to find primes manually for a small range.
Second, try to describe your algorithm.
Third, write your program with pseudo code.
Fourth, convert pseudo code to programming language.
Some tips for you:
First suggestion - all numbers from our "small" range could be prime. Remove all numbers that definitively not prime from this range. /Numbers that divided by 2, at least/
You can try to divide all items from resulting sequence by the start items /3,5,.../ if you can divide any item then remove it from sequence. If any numbers couldn't be divided by any previous then you are found all primes.
Optimization tip: It is no sense to check if 21 can be divided by 11 /21/2 = 10.5, 11>10.5/
Business Accounts
Answer for Membership
by: Infinity08Posted on 2008-01-13 at 10:11:53ID: 20648491
>> I tried many ways to solve it but it didn't work.
Can you show what you tried, and tell where exactly you are stuck ?
Since this is an assignment, we can't solve it for you, but we will help you solve it yourself ...