Skip to content

Instantly share code, notes, and snippets.

@55v
Created August 26, 2011 09:59
Show Gist options
  • Save 55v/1173112 to your computer and use it in GitHub Desktop.
Save 55v/1173112 to your computer and use it in GitHub Desktop.
(*
Problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
*)
let problem_1 n =
let mutable result = 0
let rec loop n r =
if n = 0 then r
else
if (n % 3 = 0) || (n % 5 = 0) then loop (n-1) r + n
else loop (n-1) r
loop (n-1) result
//clever but lazy one, with lists comprehensions (not mine):
let problem_1a =
([3..3..1000]@[5..5..999] |> List.sum) - ([15..15..1000] |> List.sum)
//super, from pdf attached to problem:
let problem_1b target =
let sum (n) =
let p = target / n
n*(p*(p+1)) / 2
(sum 3) + (sum 5) - (sum 15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment