473,466 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Create 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 1937
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
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...
6
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...
5
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...
7
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...
2
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...
2
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...
6
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...
1
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...
1
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...
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
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...
1
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...
0
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...
0
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.