home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / programm / 3578 < prev    next >
Encoding:
Internet Message Format  |  1993-01-24  |  1.2 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!mickey!bsmith
  2. From: bsmith@mickey.NoSubdomain.NoDomain (Brian Smith)
  3. Newsgroups: comp.programming
  4. Subject: Re: how about PRIME numbers ?
  5. Date: 25 Jan 1993 01:28:26 GMT
  6. Organization: University of California, Berkeley
  7. Lines: 22
  8. Sender: bsmith@mickey (Brian Smith)
  9. Distribution: world
  10. Message-ID: <1jvfnq$rvn@agate.berkeley.edu>
  11. References: <peterd.727578786@tscc2> <1993Jan24.055343.2392@organpipe.uug.arizona.edu>
  12. NNTP-Posting-Host: mickey.cs.berkeley.edu
  13.  
  14. In article <1993Jan24.055343.2392@organpipe.uug.arizona.edu>,
  15. dave@cs.arizona.edu (Dave Schaumann) writes:
  16. |> int is_prime( unsigned long n ) {
  17. |> 
  18. |>   unsigned long i, max = sqrt(n) ;
  19. |> 
  20. |>   if( n <= 3 ) return 1 ;
  21. |> 
  22. |>   if( n % 2 == 0 || n % 3 == 0 ) return 0 ;
  23. |>   for( i = 6 ; i <= max+1 ; i += 6 )
  24. |>     if( n % (i-1) == 0 || n % (i+1) == 0 ) return 0 ;
  25. |> 
  26. |>   return 1 ;
  27.  
  28. Slight bug.  The for loop should go to max+2, not max+1
  29.  
  30. Nice trick!
  31.  
  32. -------
  33. Brian C. Smith                arpa:  bsmith@cs.Berkeley.EDU
  34. University of California, Berkeley    uucp: uunet!ucbvax!postgres!bsmith
  35. Computer Sciences Department        phone: (510)642-9585
  36.