UGN Security Forums
My ProfileMember DirectoryLogin
Search our ForumsView our FAQView our Site Rules
View our CalendarView our Active TopicsGo to our Main Page

UGN Security Store
 

Network Sites UGN Security, Elite Web Gamers, Back of the Web, EveryDay Helper, VNC Web Design & Development
July
Su M Tu W Th F Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Our Sponsors
Latest Postings
Comcast upgrades Upload Speed!
by Gizmo
Today at 04:00 PM
Robert Moore aka MooreR PC Magazine Subscription
by Gizmo
06/22/08 01:36 PM
How to write a GUI with C++ in windows?
by Murakami Kakason
06/18/08 06:21 AM
Podcasting, ideas and interests...
by ZER0_DECEPTION
06/16/08 07:43 AM
PHP -MVC(Model, View, Control) overview
by §intå×
06/09/08 05:07 AM
Vote for President
by ZER0_DECEPTION
06/06/08 07:46 AM
Notepad++ 4.9.2 - open source text editor
by Gizmo
06/06/08 01:42 AM
Topic Options
#17181 - 04/18/04 06:37 AM c prime # generator
geodesic Offline
Junior Member

Registered: 04/18/04
Posts: 6
Loc: 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

Top
Our Sponsors
Sponsor Our Sponsors

Top  
#17182 - 04/18/04 07:16 AM Re: c prime # generator
geodesic Offline
Junior Member

Registered: 04/18/04
Posts: 6
Loc: 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

Top
#17183 - 04/18/04 09:29 AM Re: c prime # generator
SilentRage Offline
DollarDNS Owner

Registered: 03/04/02
Posts: 1273
Loc: 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

Top
#17184 - 04/18/04 11:12 AM Re: c prime # generator
geodesic Offline
Junior Member

Registered: 04/18/04
Posts: 6
Loc: The Riemann Manifold
outstanding silentrage !!

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

G

Top
#17185 - 04/18/04 11:51 AM Re: c prime # generator
Gizmo Administrator Offline
Community Owner
*****

Registered: 02/28/02
Posts: 6894
Loc: 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, Elite Web Gamers & VNC Web Design Owner

Top
#17186 - 04/29/04 01:38 AM Re: c prime # generator
ninjaneo Offline
Microwavable Pillow Tosser

Registered: 03/06/02
Posts: 229
Loc: 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.

Top
#17187 - 03/30/05 10:05 PM Re: c prime # generator
fortytwo Offline
Junior Member

Registered: 03/30/05
Posts: 1
Loc: 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.

Top



Forum Stats
6899 Members
45 Forums
10167 Topics
44858 Posts

Max Online: 677 @ 06/30/07 10:06 PM
Top Posters
Gizmo 6894
§intå× 3241
UGN Security 3113
IceMyst 1445
SilentRage 1273
Ice 1146
pergesu 1134
Infinite 1039
jonconley 954
Girlie 903
Newest Members
real-along, Murakami Kakason, die, ReduX, UknownWarrior
6898 Registered Users
Who's Online
0 Registered (), 9 Guests and 19 Spiders online.
Key: Admin, Global Mod, Mod
Latest News
IRC Server Funding
by Gizmo
07/01/08 02:48 PM
Fixed: Front Page - News Display
by Gizmo
06/25/08 11:25 AM


Donate

Get the Google FireFox Toolbar
Get Firefox!
Get FireFox!