473,748 Members | 6,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in Generating 1st 100 Primes

Hi

I was writing a simple code to generate the first 100 prime numbers.
Everything looks fine to me except the output of the program. What's
wrong with it? I am attaching the program as well as the output. Would
appreciate if someone could mail me at ba************@ gmail.com

Thanks
A M Rahman

//------------------------------------------------------------------------------------------------------------------------------------------

#include<iostre am.h>
#include<proces s.h>

int prime(int number);
int main(void)
{

int counter = 0;

for (int n = 2; n < 1000; n++)
{
if ( prime(n) == 1 )
{
cout << n << " ";
counter ++;
if (counter 99)
{
exit(0);
}

} // close if
} // close for

return 0;
} // close main

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;
}

else
{
return 0;
}
} // close for
}

-----------------------------------------------------------------------------------------------------------------------------------//

output

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101
103 105
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141
143 145
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181
183 185
187 189 191 193 195 197 199

Jul 28 '06 #1
18 2109

baltimoredude1 wrote:
int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;
}

else
{
return 0;
}
} // close for
}
This will return true when 'number' mod some number is not 0. 15 mod 2
is not zero ( so your function returns true ), but 15 is not prime.
Basically this prime function isn't implemented right.

-Brian

Jul 28 '06 #2
baltimoredude1 wrote:
I was writing a simple code to generate the first 100 prime numbers.
Are you sure about "100"? You seem to only print out first 99, no?
Everything looks fine to me except the output of the program.
LOL... So, everything is OK, only it doesn't work, eh? That means
not everything is OK.
What's
wrong with it?
A couple of things that I immediately see. Comments below...
I am attaching the program as well as the output. Would
appreciate if someone could mail me at ba************@ gmail.com

Thanks
A M Rahman

//------------------------------------------------------------------------------------------------------------------------------------------

#include<iostre am.h>
There is no such standard header. There hasn't been for as long as we had
the Standard. Why are you still writing non-standard code? Should be:

#include <iostream>
#include<proces s.h>
This is a non-standard header. Are you using anything from it, actually?
>
int prime(int number);
int main(void)
"void" between parentheses doesn't make much sense, unless you're writing
a C program. You're not. So, it doesn't. And although it's allowed, you
should take a habit of not using it.
{

int counter = 0;

for (int n = 2; n < 1000; n++)
{
if ( prime(n) == 1 )
{
cout << n << " ";
This should be

std::cout << n << " ";
counter ++;
if (counter 99)
{
exit(0);
This should probably be simply

break;
}

} // close if
} // close for

return 0;
} // close main

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;
If there is a remainder from division, shouldn't you be going on trying
to find another divisor?
}

else
{
return 0;
}
} // close for
And once you checked _all_ numbers, what do you return?
}

-----------------------------------------------------------------------------------------------------------------------------------//

output

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101
103 105
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139
141 143 145
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179
181 183 185
187 189 191 193 195 197 199
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #3
baltimoredude1 wrote:
Hi

I was writing a simple code to generate the first 100 prime numbers.
Everything looks fine to me except the output of the program. What's
wrong with it? I am attaching the program as well as the output. Would
appreciate if someone could mail me at ba************@ gmail.com

Thanks
A M Rahman

//------------------------------------------------------------------------------------------------------------------------------------------

#include<iostre am.h>
#include <iostream>
#include<proces s.h>
Not a standard header. Not used anyway.

int prime(int number);
May I recommend

bool is_prime( unsigned number );
>

int main(void)
{

int counter = 0;

for (int n = 2; n < 1000; n++)
{
if ( prime(n) == 1 )
{
cout << n << " ";
counter ++;
if (counter 99)
{
exit(0);
}

} // close if
} // close for

return 0;
} // close main

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;
}

else
{
return 0;
}
} // close for
}
Note:

a) This function never returns a value for number == 2. Thus, you have
undefined behavior in main().

b) For number 2, the prime() function is semantically broken. The for loop
never enters the second iteration, i.e., i==2 is the only case that ever
occurs: if number is not a multiple of 2, prime returns 1, if number is a
multiple of 2, prime returns 0. This matches the output.

-----------------------------------------------------------------------------------------------------------------------------------//

output

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101
103 105
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141
143 145
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181
183 185
187 189 191 193 195 197 199
Jul 28 '06 #4
baltimoredude1 wrote:
Hi

I was writing a simple code to generate the first 100 prime numbers.
Everything looks fine to me except the output of the program. What's
wrong with it? I am attaching the program as well as the output. Would
appreciate if someone could mail me at ba************@ gmail.com
It's pretty obvious what's wrong with the output-- you've printed out
every odd number greater than 2.
>
Thanks
A M Rahman

//------------------------------------------------------------------------------------------------------------------------------------------

#include<iostre am.h>
#include<proces s.h>

int prime(int number);
int main(void)
{

int counter = 0;

for (int n = 2; n < 1000; n++)
{
if ( prime(n) == 1 )
{
cout << n << " ";
counter ++;
if (counter 99)
{
exit(0);
}

} // close if
} // close for

return 0;
} // close main
Time for lesson 1 of debugging. Step through the code! Pick a value
where you're getting the wrong result and see what happens. We know 9
isn't a prime so put 9 into your function and watch what it does. When
it says 9 *is* a prime figure out what must be wrong with your logic.
int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;
}

else
{
return 0;
}
} // close for
}
Here's a hint: For a single call to prime() what's the most number of
times that the for loop can run? Remember that the function (and hence
the loop) will end as soon as a return is encountered.

>
-----------------------------------------------------------------------------------------------------------------------------------//

output

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101
103 105
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141
143 145
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181
183 185
187 189 191 193 195 197 199
Jul 28 '06 #5
Hi

Your prime function should be like below : -

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i == 0)
{
return 0;
}

} // close for

return 1;

}

I hope this willl solve your problem

Pankaj

Jul 28 '06 #6

Victor Bazarov wrote:
baltimoredude1 wrote:
I was writing a simple code to generate the first 100 prime numbers.

Are you sure about "100"? You seem to only print out first 99, no?
HMM... OUTPUT IS POSTED.. PLEASE DO THE COUNTING...
>
Everything looks fine to me except the output of the program.

LOL... So, everything is OK, only it doesn't work, eh? That means
not everything is OK.
LOOKS OK TO ME AND I KNOW IT'S NOT OK....
>
What's
wrong with it?

A couple of things that I immediately see. Comments below...
I am attaching the program as well as the output. Would
appreciate if someone could mail me at ba************@ gmail.com

Thanks
A M Rahman

//------------------------------------------------------------------------------------------------------------------------------------------

#include<iostre am.h>

There is no such standard header. There hasn't been for as long as we had
the Standard. Why are you still writing non-standard code? Should be:

#include <iostream>
#include<proces s.h>

This is a non-standard header. Are you using anything from it, actually?
MAYBE YOU SHOULD DO SOME RESEARCH ON THIS ISSUE.
>

int prime(int number);
int main(void)

"void" between parentheses doesn't make much sense, unless you're writing
a C program. You're not. So, it doesn't. And although it's allowed, you
should take a habit of not using it.
AGAIN DO SOME RESEARCH ON THIS ISSUE.
>
{

int counter = 0;

for (int n = 2; n < 1000; n++)
{
if ( prime(n) == 1 )
{
cout << n << " ";

This should be

std::cout << n << " ";
counter ++;
if (counter 99)
{
exit(0);

This should probably be simply

break;
}

} // close if
} // close for

return 0;
} // close main

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i != 0)
{
return 1;

If there is a remainder from division, shouldn't you be going on trying
to find another divisor?
ANYWAY I FIGURED OUT THE PROBLEM AND IT'S WORKING FINE.
>
}

else
{
return 0;
}
} // close for

And once you checked _all_ numbers, what do you return?
}

-----------------------------------------------------------------------------------------------------------------------------------//

output

2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101
103 105
107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139
141 143 145
147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179
181 183 185
187 189 191 193 195 197 199

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 29 '06 #7
I figured it out and it's working fine

Panks wrote:
Hi

Your prime function should be like below : -

int prime(int number)
{

for (int i = 2; i <= number -1 ; i++)
{
if (number % i == 0)
{
return 0;
}

} // close for

return 1;

}

I hope this willl solve your problem

Pankaj
Jul 29 '06 #8
baltimoredude1 wrote:
[..]
HMM... OUTPUT IS POSTED.. PLEASE DO THE COUNTING...
[..]
LOOKS OK TO ME AND I KNOW IT'S NOT OK....
[..]
MAYBE YOU SHOULD DO SOME RESEARCH ON THIS ISSUE.
[..]
AGAIN DO SOME RESEARCH ON THIS ISSUE.
[..]
ANYWAY I FIGURED OUT THE PROBLEM AND IT'S WORKING FINE.
For a netiquette violator, you're being too polite. You need
to call me an ass or something. To complete the picture...
Jul 29 '06 #9
I guess it's more efficient!
............... ....
bool is_Prime( unsigned candidate )
{
if ( candidate == 2 )
return ( true );
int lim = sqrt( ( double )candidate );
for ( int index = 3; index <= lim; index += 2 )
if ( candidate % index == 0 )
return ( false );
return ( ( candidate % 2 ) );
}
............... ...

Jul 29 '06 #10

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

Similar topics

0
2412
by: AdrianK | last post by:
I'm having a lotta problems installing Crypt::RSA on Linux Linux gogol 2.4.17smt-mono using perl5.005_03. Main problem at present is that all the tests fail with Crypt::Primes When I run a trace it looks like a segmentation fault (The same fault I am getting if I force install and run a keygen using Crypt::RSA). Any ideas/help would be appreciated. Crypt::Primes (make test)
32
5117
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 * 10^12) it's pretty efficient, it took 15 minutes to compute the first 1,000,000 primes.
15
1807
by: interpim | last post by:
Im trying to teach myself programming, and I am doing exercises out of the book C by Dissection by Ira Pohl. I am stuck on one of them and can't figure out how to fix it. The exercise is for you to write a program that asks for a user inputted number and then print out that many prime numbers. The function that determines if a number is prime or not is provided. My problem is that I can't figure out how to call the function repetitively...
104
5172
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
26
2566
by: Schizoid Man | last post by:
Hi, I have a very strange arithmetic problem in C: double t = 0.1; int steps = 10; double time_step = t / (double)steps; I would expect the output of time_step to be 0.01000 (my output is of the form %5.5f), but instead it a very large (and incorrect) negative
14
24005
by: hey77 | last post by:
how can i calculate all the twin primes, and then add up how many there are? a prime is any number than can only be divised by 1 and itself, and a twin prime is two primes seperated by 2 ( for example 3 and 5) here is code for finding a prime if it helps #include <iostream> using namespace std; int main () { int prime, range=1000; int n; for ( prime = 2; prime<=range; prime++ )
5
2449
by: Carramba | last post by:
theorem states that: Integer n is prime if and only if (x +1)^n ≡ x^n +1 (mod n) in Z. so I testing it, but values doesn't match ... and I don't se why.. I guess :) it's some thing wrong in my implementation... hope you can help out :) #include <stdlib.h> #include <stdio.h>
7
2509
by: Umesh | last post by:
Write a program for this purpose. Note: If the difference of two consecutive primes is 2, they are called twin primes. e.g. 3 & 5, 11 & 13 etc.
9
1886
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 () { int a, b, c, d;
0
8987
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
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
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...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
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();...
1
3303
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

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.