2 people like it.

Farey Sequence

Return a sequence that contains the numerators and denominators as tuples for Farey Sequence n.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
let farey n =
    seq {
        let p = ref 0
        let q = ref 1
        let p' = ref 1
        let q' = ref n
        yield (!p, !q)
        while not (!p = 1 && !q = 1) do
            let c = (!q + n) / !q'
            let p'' = c * !p' - !p
            let q'' = c * !q' - !q
            p := !p'
            q := !q'
            p' := p''
            q' := q''
            yield (!p, !q) }
val farey : n:int -> seq<int * int>

Full name: Script.farey
val n : int
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val p : int ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val q : int ref
val p' : int ref
val q' : int ref
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val c : int
val p'' : int
val q'' : int
Raw view Test code New version

More information

Link:http://fssnip.net/2L
Posted:13 years ago
Author:Graham Spiers
Tags: sequences , farey