Your browser does not seem to support CSS. If images appear below, please disregard them.
It appears that you're running an Ad-Blocker. This site is monetized by Advertising and by User Donations; we ask that if you find this site helpful that you whitelist us in your Ad-Blocker, or make a Donation to help aid in operating costs.
Previous Thread
Next Thread
Print Thread
Rate This Thread
#17181 - 04/18/04 10:37 AM c prime # generator  
Joined: Apr 2004
Posts: 6
geodesic Offline
Junior Member
geodesic  Offline
Junior Member

Joined: Apr 2004
Posts: 6
The Riemann Manifold
hello.

so far i have this program to generate the prime numbers (numbers whose only factors are 1 and themselves) between 1 and 100.

what's wrong with this picture?
Code:
#include <stdio.h>

main
{
  int i,j,k,l=0;
	
	for i=2; i<=20; ++i
	{
	   for j=1; j<=i ; ++j
	   {
	     k = i%j;
	    
            if k == 0 
	    { l = ++l; 
	    }  
	    if l == 2
            {printf "%d is a prime \n",i
            }       
           }
        } 
	
	return 0;
}
thanks for the tips / suggestions

G

Sponsored Links
#17182 - 04/18/04 11:16 AM Re: c prime # generator  
Joined: Apr 2004
Posts: 6
geodesic Offline
Junior Member
geodesic  Offline
Junior Member

Joined: Apr 2004
Posts: 6
The Riemann Manifold
well i solved it in this manner
Code:
#include <stdio.h>

int main(void)
{
	int i,j,l=0;
	
	while i <= 100
	{
	   l = 0;
	   
	   for j=1; j<=i; j++
	   {
	    if i%j == 0
	    {
	     l++;
	    }
	   
	   }
	   
	   if l == 2
	   {
	    printf "%d is a prime number\n" , i;
	   } 
	   
	   i++;
	}  
	
	return 0;
}
rather brutish etc. is there any way to stremline it .. like any way to use for loops instead of the while ...

thanks

G

#17183 - 04/18/04 01:29 PM Re: c prime # generator  
Joined: Mar 2002
Posts: 1,273
SilentRage Offline
DollarDNS Owner
SilentRage  Offline
DollarDNS Owner

Joined: Mar 2002
Posts: 1,273
OH, USA
I've changed the while into a for like you asked, and also made the algorithm twice as fast by skipping the evaluation of even numbers which are all non-prime. Actually it's more than twice as fast with several other optimizations I made.

Code:
#include <stdio.h>

int main(void)
{
    int i=0,j=0,l=0;

    printf("1 is a prime number\n");
    printf("2 is a prime number\n");

    for (i=3; i < 101; i=i+2)
    {
        l = 0;

        // skip 1, don't go as high as i
        for (j=2; j<i; j++)
        {
            if (i % j == 0)
            {
                l++;
                // we already know it isn't prime
                break; 
            }
        }

        if (l == 0)
        {
            printf("%d is a prime number\n", i);
        }
    }

    return 0;
}


Domain Registration, Hosting, Management
http://www.dollardns.net
#17184 - 04/18/04 03:12 PM Re: c prime # generator  
Joined: Apr 2004
Posts: 6
geodesic Offline
Junior Member
geodesic  Offline
Junior Member

Joined: Apr 2004
Posts: 6
The Riemann Manifold
outstanding silentrage !!

that is MUCH faster and more elegant. thank you very much !!

G

#17185 - 04/18/04 03:51 PM Re: c prime # generator  
Joined: Feb 2002
Posts: 7,195
Gremelin Offline
Community Owner
Gremelin  Offline

Community Owner

Joined: Feb 2002
Posts: 7,195
Likes: 3
Portland, OR; USA
I edited the original posts, mainly to add the code tags so that formatting was done properly.

Thanks for the code though, we were actually toying with some encryption a while ago ...


Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner
Sponsored Links
#17186 - 04/29/04 05:38 AM Re: c prime # generator  
Joined: Mar 2002
Posts: 256
ninjaneo Offline
UGN Security Staff
ninjaneo  Offline
UGN Security Staff

Joined: Mar 2002
Posts: 256
CA, USA
When I made my prime generator, I did it a different way...

If you take your range of numbers... lets say 20, starting with one. go like so...

Well, we know 1 is a prime. so skip that.
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

(things that we've called primes are in bold, composites are in italics)

now, we will eliminate every multiple of 2.

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

ok, now we see that three is the next prime (because its the next not elimanated number).
So, lets kill multiples of 3.

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

mmk, Most of those we already eliminated - but so what.

5 is next

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

ok, well that didn't do much.

Well, now that its 11's turn and that is more than half of the range of numbers (20/2==10) there is no reason to check the rest.

Any non-eliminated number is prime.

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

hmmm... those italics didn't show up very well... oh well.

#17187 - 03/31/05 03:05 AM Re: c prime # generator  
Joined: Mar 2005
Posts: 1
fortytwo Offline
Junior Member
fortytwo  Offline
Junior Member

Joined: Mar 2005
Posts: 1
Boston
I just stored the primes in an array as I found them. To find them you just see if the number can be divided by any of the primes you have already found.


Member Spotlight
Gremelin
Gremelin
Portland, OR; USA
Posts: 7,195
Joined: February 2002
Show All Member Profiles 
Forum Statistics
Forums45
Topics47,470
Posts82,640
Average Daily Posts8
Members2,159
Most Online1,567
Apr 25th, 2010
Latest Postings
Top Posters(All Time)
UGN Security 40,634
Gremelin 7,195
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Liked Users (All Time)
§intå× Likes: 1
Cold Sunn Likes: 1
Crime Likes: 1
Cyrez Likes: 1
Ghost Likes: 1
Gremelin Likes: 4
Ice Likes: 1
unreal Likes: 1
Top Liked Users (30 Days)
No Data Found
Powered by UBB.threads™ PHP Forum Software 7.6.0
(Snapshot build 20160902)