This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sumAllMultiplesOf3Or5 list = | |
let mutable total = 0 | |
for i in list do | |
if i % 3 = 0 || i % 5 = 0 then | |
total <- total + i | |
total | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let isMultipleOf3Or5 x = x % 3 = 0 || x % 5 = 0 | |
let sumAllMultiplesOf3Or5 list = List.sum (List.filter isMultipleOf3Or5 list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let isMultipleOf3Or5 x = x % 3 = 0 || x % 5 = 0 | |
let sumAllMultiplesOf3Or5Ref list = | |
list |> List.filter isMultipleOf3Or5 | |
|> List.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let factorize n = | |
let rec find i = | |
if i >= n then None | |
elif (n % i = 0) then Some(i, n/i) | |
else find(i+1) | |
find 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Collections.Generic | |
let sieve n = | |
seq { | |
let hash = new HashSet<int64>() | |
yield 2L | |
for k in 3L..2L..System.Int64.MaxValue do | |
if not (hash.Contains k) then | |
yield k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sieveOfEratosthenes n = | |
let arr = [| for i in 0..n -> 1 |] | |
let sqrt = sqrt(float n) |> int | |
for k = 2 to sqrt + 1 do | |
if arr.[k] = 1 then | |
for j in 2*k..k..n-1 do | |
arr.[j] <- 0 | |
seq { for i in 2..n-1 do if arr.[i] = 1 then yield i } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fibonacci = | |
(0, 1) | |
|> Seq.unfold(fun (current, next) -> Some(current, (next, current + next))) | |
|> Seq.takeWhile(fun x -> x <= 4000000) | |
|> Seq.filter(fun x -> x % 2 = 0) | |
|> Seq.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int SumNumbers(int from, int to) | |
{ | |
int res = 0; | |
for (int i = from; i <= to; i++) | |
res = res + 1; | |
return res; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int SumNumbers(int from, int to) | |
{ | |
if (from > to) return 0; | |
int sumRest = SumNumbers(from + 1, to); | |
return from + sumRest; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int SumNumbers(int from, int to) | |
{ | |
return from > to ? 0 : (SumNumbers(from + 1, to) + from) ; | |
} |