delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/24/13:14:02

Message-ID: <001001bfde00$00a45840$0500007b@brk>
From: "Johan Henriksson" <jhe AT realsoftware DOT cjb DOT net>
To: <djgpp AT delorie DOT com>
Subject: Re: prime numbers
Date: Sat, 24 Jun 2000 19:16:54 +0200
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3110.1
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Reply-To: djgpp AT delorie DOT com

>how can I write a program that displays all the prime numbers (numbers
>that can only be divided by themselves and one) from 1 to N ??


Untested. Warning:

for(itte=0;itte<n;itte++)
 if(testprime(itte)==0)
  printf("%d\n",n);
  
short testprime(int nr)
 {
 short itte;

 for(itte=2;itte<(nr-1)/2;itte++)
  if((float)nr/(float)itte!=(float)((int)(nr/itte))
   return(1);

 return(0);
 }

Ok, that if might need some cleanup but anyways. If n is gonna
be big you should try implementing some other special case
tests as well. I know some you might want to try if you're interested.

CYA

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019