4 people like it.

Project Euler #1

This snippet is code that solves first Project Euler problem. It finds the sum of all the multiples of 3 or 5 below 1000. Please add other (more efficient, succinct or interesting) solutions to this snippet.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
let  range     = [0..999]
let  sumFunc   = List.fold (+) 0
let  condition = fun  x ->  x % 3 = 0 || x % 5 = 0
List.filter condition range |> sumFunc |> System.Console.Write

// An improvement, as requested:

[0..999] 
|> Seq.filter (fun x -> x % 3 = 0 || x % 5 = 0) 
|> Seq.reduce (+)
val range : int list

Full name: Script.range
val sumFunc : (int list -> int)

Full name: Script.sumFunc
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 fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val condition : x:int -> bool

Full name: Script.condition
val x : int
val filter : predicate:('T -> bool) -> list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
namespace System
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
System.Console.Write(value: string) : unit
   (+0 other overloads)
System.Console.Write(value: obj) : unit
   (+0 other overloads)
System.Console.Write(value: uint64) : unit
   (+0 other overloads)
System.Console.Write(value: int64) : unit
   (+0 other overloads)
System.Console.Write(value: uint32) : unit
   (+0 other overloads)
System.Console.Write(value: int) : unit
   (+0 other overloads)
System.Console.Write(value: float32) : unit
   (+0 other overloads)
System.Console.Write(value: decimal) : unit
   (+0 other overloads)
System.Console.Write(value: float) : unit
   (+0 other overloads)
System.Console.Write(buffer: char []) : unit
   (+0 other overloads)
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val reduce : reduction:('T -> 'T -> 'T) -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.reduce

More information

Link:http://fssnip.net/1a
Posted:5 years ago
Author:Eugene Gavrin
Tags: list , filter , fold , euler problem , inclusion-exclusion