473,815 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on program

15 New Member
Can someone please help me with this program.

How would i go about writing a program which determines the prime numbers from 2 to 1000 in increasing order using the “sieve method of Eratosthenes”. AND Display the values determined to be prime for the user at the display device
Dec 4 '06 #1
9 1955
sicarie
4,677 Recognized Expert Moderator Specialist
Can someone please help me with this program.

How would i go about writing a program which determines the prime numbers from 2 to 1000 in increasing order using the “sieve method of Eratosthenes”. AND Display the values determined to be prime for the user at the display device
Sure we can help - what do you have so far? Please post all your code and any compiler error messages.
Dec 4 '06 #2
k1ckthem1dget
15 New Member
I dont have much yet, i need help starting. Then i will see what happens, and post back with questions.
Thanks
Dec 4 '06 #3
sicarie
4,677 Recognized Expert Moderator Specialist
I dont have much yet, i need help starting. Then i will see what happens, and post back with questions.
Thanks
Well, to start it - can you tell me how you would do this, in a few sentences?
Dec 4 '06 #4
DeMan
1,806 Top Contributor
The “sieve method of Eratosthenes” (as far as I know), is the idea that you start with a list of numbers, then remove all the multiples of the first number then the second (remaining) number etc so we would start with:
2,3,4,5,6,7,8,9 ,10,11,12,13,14 ,15,......,1000
and we first remove multiples of 2 (except itself) leaving:
2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, ......999
Then we look at the next number (3) and remove all multiples of him:
2, 3, 5, 7, 11, 13, 17, 19, 23, 25, 29, .......997
Next we go for 5
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,............ 997
etc.

Begin by setting up a list structure (you could use an array if you are unfamiliar with other structures, but personally I don't like this idea, because it means you would still iterate over 1000 elements (999 actually) to print only the primes leftover by going through the set several times already).

then (in a javanese dialect of pseudocode):
Expand|Select|Wrap|Line Numbers
  1. counter=0;
  2. while myList has more elements
  3. {
  4.   currentElement=myList.elementAt(counter)
  5.   counter++;
  6.   counter2=counter
  7.   while(counter2<myList.size())
  8.   {
  9.     if ((myList.elementAt(counter2) mod currentElement)==0)
  10.     {
  11.       myList.removeElement(counter2);
  12.     }
  13.     else
  14.     {
  15.       counter2++
  16.     }
  17.   }
  18. }
  19.  
then print out the list.
I'm not certain the logic above is correct, in particular counter2 (When you remove an element you don't need to increment it <convince yourself of this, draw a picture of what's happening if need be>).

Another way to approach program (which I would still argue uses the idea of the sieve of Eratosthenes (though your teacher/lecturer may not agree)) would use 2 lists:
The first one starts with all the numbers from 2..1000, the second starts empty.
We can then iterate through the elements in the first list, only adding it to the second list if it cannot be divided by any of the elements in the second list. (I initially though this was more efficient, but have changed my mind (well implemented it would have a similar efficiency to the code above)).

thus the logic would be more like:
Expand|Select|Wrap|Line Numbers
  1. for(int i=2; i<firstList.size(); i++)
  2. {
  3.   boolean prime=true;  //some will say this is bad logic (and their kinda right)
  4.   for(int j=0;j<secondList.size(); j++)
  5.   {
  6.     if(firstList.elementAt(i) mod secondList.elementAt(j) == 0)
  7.     {
  8.       prime=false;
  9.       break;  //out of the loop
  10.     }
  11.   }
  12.   if(prime)
  13.   {
  14.     secondList.add(firstList.elementAt(i));
  15.   }
  16. }
  17.  
  18.  
I leave it to you to
a) make sure this is indeed the sieve of E
b) follow the logic to ensure it is correct
c) turn it into c (or whichever language you are using)
Dec 4 '06 #5
shamik
5 New Member
the above one seems to be quite difficult to understand
here is a very simple logic.

define an integer flag to keep track and initalize it.
int flag = 1;

now start a loop from starting no. i.e 2 till half of the ending no. i.e 500 (half of 1000) because no number greater than 500 would be able to devide 1000 exactly.

like this :

for(i=2;i<=500; i++)

then start another loop inside this starting from 2 till i .....like this :

for(j=2;j<=i;j+ +)

then devide i by j.... if ( i%j == 0)

then increment flag by 1.........flag+ +

close the if statement...... .....like this:

if ( i%j == 0)
{
flag++;
}

then close the for loop for j

now here if the value of flag>2 then its not a prime no.
so if the value of flag=2 then print the value because it is prime.

again make flag =0;

now close the for loop of i

you are done.

thanks and regards
shamik
Dec 5 '06 #6
shamik
5 New Member
the whole program is like this :

int flag = 1;

for ( i=2;i<1000;i++)
{
for( j=2;j<=i;i++)
{
if(i%j==0)
{
flag++;
}
}
if(flag<=2)
{
cout<<i<<endl;
}
flag = 0;
}

finish

The above post might have some mistakes coz its written to make u understand the logic. note that this is a just a logic of the code and not the entire program. it might vary depending on ur requirements.


thanks and regards.
shamik
Dec 5 '06 #7
willakawill
1,646 Top Contributor
the whole program is like this :

int flag = 1;

for ( i=2;i<1000;i++)
{
for( j=2;j<=i;i++)
{
if(i% j==0)
{
flag++;
}
}
if(flag<=2)
{
cout<<i<<endl;
}
flag = 0;
}

finish

The above post might have some mistakes coz its written to make u understand the logic. note that this is a just a logic of the code and not the entire program. it might vary depending on ur requirements.


thanks and regards.
shamik
Expand|Select|Wrap|Line Numbers
  1. bool prime = true;
  2. cout<<2<<endl;
  3.  
  4. for ( i = 3; i < 1001; i++ )
  5. {
  6.     for( j = 2; j < i; j++ )
  7.     {
  8.         if( i % j == 0 )
  9.         {
  10.             prime = false;
  11.         }
  12.     }
  13.     if(prime)
  14.     {
  15.         cout<<i<<endl;
  16.     }
  17.     prime = true;
  18. }
slightly modified to include # 1000 and fix some typos :)
Dec 5 '06 #8
willakawill
1,646 Top Contributor
this is a little more 'cut & paste' for you and covers #2 in the algorithm in a sneaky way for you to discover :)

Expand|Select|Wrap|Line Numbers
  1.                 bool prime = true;
  2.  
  3.     for (int i = 2; i < 1001; i++ )
  4.     {
  5.         for(int j = 2; j < i; j++ )
  6.         {
  7.             if( i % j == 0 )
  8.             {
  9.                 prime = false;
  10.             }
  11.         }
  12.         if(prime)
  13.         {
  14.             cout<<i<<endl;
  15.         }
  16.         prime = true;
  17.     }
Dec 5 '06 #9
k1ckthem1dget
15 New Member
Thanks alot, i will post back in a few days with my code if i have any problems. Thanks again.
Dec 5 '06 #10

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

Similar topics

4
2761
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like storing products in
6
4360
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program) //-------------------------------------------------------------------------- - #pragma hdrstop #pragma argsused
5
3017
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my homework for me.. I am merely asking for help, perhaps pointers as to where to begin.. I've never used access before.. I'm rather cluey when it comes to
7
3303
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that I believe if it's solved, the remaining stuff is easy... my full program until now is here: http://www.geocities.com/tom4_h4wk/progmail.zip the problem is the segmentation fault when main trys to run leficheiro.c.... the *.c2 files are the...
2
2127
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before my other includes then I get lots of errors like C2011: 'fd_set' : 'struct' type redefinition warning C4005: 'FD_CLR' : macro redefinition which I understand are due to the fact that windows.h is being included in another header file as well as...
2
2147
by: Bsnpr8 | last post by:
I need help guys, i have to many stuff to do, because i am in my last 2 weeks of the university, my last assignment is to do a spell checker in C++, i have an idea but nothing is coming out. I really need help, could someone do, anything to help me i don't have much time, here are the instructions: Instructions: In this project, you are asked to develop your own spell-checker utility. We make suggestions to help get you started. You should...
6
1649
by: HelpME | last post by:
I wrote a program in Vb.Net that was running fine. However I am unable to install it on a couple of machines. When i run it I get a windows error message that says My Project.exe has encountered a problem and needs to close. We are sorry for the inconvience. If you were in the middle of something, the information you were working on might be lost
1
1934
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at http://oldmill.uchicago.edu/~wilder/Code/random/. Firstly, I got the notorious "fatal error C1010: unexpected end of file while looking for precompiled header directive" error when I tried to compile 'randtest.c' provided by wilder.
1
1930
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at http://oldmill.uchicago.edu/~wilder/Code/random/. Firstly, I got the notorious "fatal error C1010: unexpected end of file while looking for precompiled header directive" error when I tried to compile 'randtest.c' provided by wilder.
0
9735
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
10672
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
10408
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
10427
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
9225
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...
1
7687
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...
1
4358
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
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
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.