473,394 Members | 1,840 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,394 software developers and data experts.

Another prime question

Hello everyone. I am writing a program which must print out prime numbers between 2 and a number entered by user. My code is below. Right now I am printing all odd numbers, but I can't seem to figure out how to get rid of numbers like 9,15,21, etc.

any suggestions would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int Check(int Prime[], int x)
  4. {
  5.     int j=2,t=0;
  6.     for (j=2;j<=x;++j)
  7.     {
  8.         if (j%2==0)
  9.             Prime[j]=0;
  10.         else
  11.             Prime[j]=j;
  12.         Prime[2]=2;
  13.         cout<<Prime[j]<<endl;
  14.     }
  15.     return Prime[j];
  16. }
  17. void main ()
  18. {
  19.     int i=0, Prime[100],n=0;
  20.     for (i=0;i<=99;++i)
  21.     {
  22.         Prime[i]=1;
  23.     }
  24.     cout<<"Please enter a number."<<endl;
  25.     cin>>n;
  26.     Check (Prime,n);
  27.  
  28. }
Jun 7 '07 #1
4 1472
Savage
1,764 Expert 1GB
Hello everyone. I am writing a program which must print out prime numbers between 2 and a number entered by user. My code is below. Right now I am printing all odd numbers, but I can't seem to figure out how to get rid of numbers like 9,15,21, etc.

any suggestions would be appreciated.

#include <iostream>
using namespace std;
int Check(int Prime[], int x)
{
int j=2,t=0;
for (j=2;j<=x;++j)
{
if (j%2==0)
Prime[j]=0;
else
Prime[j]=j;
Prime[2]=2;
cout<<Prime[j]<<endl;
}
return Prime[j];
}
void main ()
{
int i=0, Prime[100],n=0;
for (i=0;i<=99;++i)
{
Prime[i]=1;
}
cout<<"Please enter a number."<<endl;
cin>>n;
Check (Prime,n);

}
It's because u are using j as array index,u should use other index which will only increase if conditon is fullfield.Also u should use nested loops.

Savage
Jun 7 '07 #2
JosAH
11,448 Expert 8TB
Create a bit array of 'n' (or a bit more) bits where 'n' is the maximum number
supplied by the user.

Set all bits to one (1) except the first two bits. All bits act as a boolean flag:
if bit i equals 1, i is supposed to be a prime number.

Start at 2 and wipe out all multiples or two larger than two (4, 6, 8, etc).
Move on to the next bit that is still equal to one (1). It's bit #3; wipe out all
multiples of three larger than three (6, 9, 12, 15 etc.)
Move to the next bit equal to one and do the same over and over again until
you're at the 'n'th bit. This is the 'Sieve of Erasthostenes' algorithm.

kind regards,

Jos
Jun 7 '07 #3
Hey Jos. Thanks for the suggestion. I've wiped out the multiples of 2. I guess I'm not quit sure how to take out the multiples of 3>3. The person right before you suggested a nested loop, but I don't know where to put it.

Thanks,

J
Jun 7 '07 #4
JosAH
11,448 Expert 8TB
Hey Jos. Thanks for the suggestion. I've wiped out the multiples of 2. I guess I'm not quit sure how to take out the multiples of 3>3. The person right before you suggested a nested loop, but I don't know where to put it.

Thanks,

J
Something like this (in pseudo code):
Expand|Select|Wrap|Line Numbers
  1. for (int i= 2; i <= n; i++)
  2.    if (bit[i] is set)
  3.       for (j= i+i; j <= n; j+= i;)
  4.          wipeout(j);
  5.  
kind regards,

Jos
Jun 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

36
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,...
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...
6
by: ckroom | last post by:
Does anyone know a good algorithm to get the next prime of a number n? prime = nextprime( n ); It's for some stuff about double rehashing, thanks. -- ckroom http://nazaries.net/~ckroom
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
3
by: smayon | last post by:
Here's the problem- Write a C++ program that prompts the user to input a positive integer then output the number and a message indicating whether the number is a prime number. I found this...
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 ?
5
by: trillianx | last post by:
I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: # Find the prime numbers # This is a user input program that lets you...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.