Previous Thread
Next Thread
Print Thread
Rate Thread
#17181 04/18/04 03:37 AM
Joined: Apr 2004
Posts: 6
G
Junior Member
OP Offline
Junior Member
G
Joined: Apr 2004
Posts: 6
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

#17182 04/18/04 04:16 AM
Joined: Apr 2004
Posts: 6
G
Junior Member
OP Offline
Junior Member
G
Joined: Apr 2004
Posts: 6
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 06:29 AM
Joined: Mar 2002
Posts: 1,273
DollarDNS Owner
Offline
DollarDNS Owner
Joined: Mar 2002
Posts: 1,273
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 08:12 AM
Joined: Apr 2004
Posts: 6
G
Junior Member
OP Offline
Junior Member
G
Joined: Apr 2004
Posts: 6
outstanding silentrage !!

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

G

#17185 04/18/04 08:51 AM
Joined: Feb 2002
Posts: 7,203
Likes: 11
Community Owner
Offline
Community Owner
Joined: Feb 2002
Posts: 7,203
Likes: 11
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 wink ...


Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner
#17186 04/28/04 10:38 PM
Joined: Mar 2002
Posts: 256
Likes: 1
UGN Security Staff
Offline
UGN Security Staff
Joined: Mar 2002
Posts: 256
Likes: 1
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/30/05 08:05 PM
Joined: Mar 2005
Posts: 1
F
Junior Member
Offline
Junior Member
F
Joined: Mar 2005
Posts: 1
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.


Link Copied to Clipboard
Member Spotlight
Posts: 43
Joined: November 2002
Forum Statistics
Forums41
Topics33,840
Posts68,858
Average Daily Posts1
Members2,176
Most Online3,253
Jan 13th, 2020
Latest Postings
Where and how do you torrent?
by danni75 - 03/01/24 05:58 AM
Animation,
by JohanKaariainen - 08/15/19 01:18 AM
Blackbeard.....
by Gremelin - 10/03/18 07:02 PM
my old account still exists!
by Crime - 08/10/18 02:47 PM
Okay WTF?
by HenryMiring - 09/27/17 01:45 AM
The History Thread...
by Gremelin - 08/11/17 12:11 PM
My friend NEEDS your HELP!
by Lena01 - 07/21/17 12:06 AM
I'm having fun with this guy.
by gabithompson730 - 07/20/17 01:50 AM
I want to upgrade my phone
by gabithompson730 - 07/20/17 01:49 AM
Doom 3
by Cyrez - 09/11/14 08:58 PM
Amazon Gift Card Generator/KeyGen?te
by Gecko666 - 08/22/14 09:21 AM
AIM scene 99-03
by lavos - 09/02/13 08:06 AM
Planetside 2
by Crime - 03/04/13 07:10 AM
Beta Testers Wanted
by Crime - 03/04/13 06:55 AM
Hello Everyone
by Gremelin - 02/12/12 06:01 PM
Tracfone ESN Generator
by Zanvin Green - 01/18/12 01:31 PM
Python 3 issue
by Testing - 12/17/11 09:28 PM
tracfone airtime
by Drache86 - 07/30/11 03:37 AM
Backdoors and the Infinite
by ZeroCoolStar - 07/10/11 03:52 AM
HackThisZIne #12 Releaseed!
by Pipat2 - 04/28/11 09:20 PM
gang wars? l33t-wars?
by Gremelin - 04/28/11 05:56 AM
Consolidate Forums
by diggin2deep - 04/21/11 10:02 AM
LAN Hacking Noob
by Gremelin - 03/12/11 12:42 AM
Top Posters
UGN Security 41,392
Gremelin 7,203
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Likes Received
Ghost 2
unreal 1
Crime 1
Ice 1
Dartur 1
Powered by UBB.threads™ PHP Forum Software 7.7.5