3 people like it.

Euler #5

Euler #5 solution

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
let multiplesOf x = Seq.initInfinite (fun i -> (i+1) * x)

let leastCommonDenominator x y =
    Seq.find (fun i -> i%x = 0) (multiplesOf y)

let problem5 n =
    let testDenominator lcd =
        List.forall (fun i -> lcd % i = 0) [n-2 .. -1 .. 1]

    let rec problem5Helper (x, lcd) =
        match testDenominator lcd with
            | true -> lcd
            | false ->
                let lcd2 = leastCommonDenominator (x-1) (x-2)
                problem5Helper (x-1, (leastCommonDenominator lcd lcd2))
    
    problem5Helper (n, (leastCommonDenominator n n-1))
val multiplesOf : x:int -> seq<int>

Full name: Script.multiplesOf
val x : int
module Seq

from Microsoft.FSharp.Collections
val initInfinite : initializer:(int -> 'T) -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.initInfinite
val i : int
val leastCommonDenominator : x:int -> y:int -> int

Full name: Script.leastCommonDenominator
val y : int
val find : predicate:('T -> bool) -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.find
val problem5 : n:int -> int

Full name: Script.problem5
val n : int
val testDenominator : (int -> bool)
val lcd : int
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 problem5Helper : (int * int -> int)
val lcd2 : int
Raw view New version

More information

Link:http://fssnip.net/7u
Posted:5 years ago
Author:Michael Falanga
Tags: euler problem , algorithm , recursion , lists , learning f# , puzzles , seq