Wednesday, December 7, 2011

Sieve of Eratosthenes in F# - Attempt # 1

sieve.fs :
namespace sieve
  module sieve = 

(*
A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself.
To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
*)

    let shake (n : int) = 

      printfn "Applying Sieve of Eratosthenes to First %i Natural Numbers" n |> ignore
      
      // 1. Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n).
      let matrix = Array.init n (fun x -> true)

      // 2. Initially, let p equal 2, the first prime number.
      let mutable p = 2

      let mutable set_exhausted = false
      while (set_exhausted = false) do
      
        //  3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list.
        //  These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.        
        let mutable i = p + p
        while (i <= n) do        
          matrix.[i - 1] <- false
          i <- i + p

        //  4. Find the first number greater than p in the list that is not marked; let p now equal this number (which is the next prime).        
        p <- p + 1
        while ((p <= n - 1) && (matrix.[p - 1] <> true)) do
          p <- p + 1      
        
        //  5. If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3.
        if (p >= n - 1) then
          set_exhausted <- true

      //  6. When the algorithm terminates, all the numbers in the list that are not marked are prime.
      matrix
Program.fs :
namespace effsharp
module Main =
    open System
    open sieve
        []
        let main args =         
            
            printfn "Sieve up to what natural number ? [Max %i]" Int32.MaxValue
            let nStr = Console.ReadLine()
            let n = Int32.Parse(nStr)

            let primes = sieve.shake n

            for i = 1 to n do              
              if (primes.[i - 1] = true) then
                printfn "%i" i |> ignore

            printfn "any key to exit..."
            let endofapp = Console.ReadKey()
            0

No comments:

Post a Comment

comment: