3 people like it.

A simple sieve

A simple implementation for the sieve of Eratosthenes.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
let isPrime num sieve =
    sieve |> List.forall (fun x -> num % x <> 0)

let prime upper =
    let rec makeSieve ls acc =
        match ls with
        | x::xs when isPrime x acc -> makeSieve xs (x::acc)
        | x::xs                    -> makeSieve xs acc
        | _                        -> acc
    let nums = [2..upper]
    makeSieve nums []
val isPrime : num:int -> sieve:int list -> bool

Full name: Script.isPrime
val num : int
val sieve : int list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val forall : predicate:('T -> bool) -> list:'T list -> bool

Full name: Microsoft.FSharp.Collections.List.forall
val x : int
val prime : upper:int -> int list

Full name: Script.prime
val upper : int
val makeSieve : (int list -> int list -> int list)
val ls : int list
val acc : int list
val xs : int list
val nums : int list

More information

Link:http://fssnip.net/b4
Posted:12 years ago
Author:Gab_km
Tags: primes , sieve , eratosthenes , algorithm