Your program should simply skip even numbers, because, aside from −2 and 2, they're all composite. Checking if the digit is 0, 2 or 4 or 6 or 8 then skipping the numberĮlse calculating the square root of the number.The last digit? In what base? Base 10? I think this might be your problem. Calculating the last digit of the number to be checked.You already knew, and working on this program would have helped confirm this. I am convinced that including it was an honest mistake. Hard-coding 3 might save, at most, a millisecond. Adding 1 2 3 to the prime list initiallyĪctually, just 2 is sufficient.It should be fairly easy to take the same approach in Java. This program can be altered to generate prime numbers on-demand as well. Each time a new prime number is discovered, a filter is added to the stream so that the remainder of the stream gets filtered of any multiples of that prime number. N :: primes' (count' - 1) (filter s n) in Let rec primes' count' s = match count' with (* Get next prime, apply a new filter by that prime to the remainder of the stream *) (* Filter the given stream of any multiples of x *) Here's an Ocaml program that implements the Trial division sieve (which is sort of the inverse of Eratosthenes as correctly pointed out by Will): (* Creates a function for streaming integers from x onward *)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |