Skip to content

Instantly share code, notes, and snippets.

@JonCanning
Last active December 29, 2015 03:29
Show Gist options
  • Save JonCanning/7607687 to your computer and use it in GitHub Desktop.
Save JonCanning/7607687 to your computer and use it in GitHub Desktop.
Project Euler
Useful functions:
Convert number to array of numbers:
Seq.unfold (function | x when x = 0 -> None | x when x > 10 -> Some(x / 10, x % 10) | x -> Some(x, 0)) 81
Primes:
Seq.filter (fun x -> {2..x |> float |> sqrt |> int} |> Seq.forall(fun y -> x = 2 || x % y > 0))
-----------------------------------------------
1.
{1..999} |> Seq.filter (fun x -> x % 3 = 0 || x % 5 = 0) |> Seq.sum;;
2.
Seq.unfold (fun (x,y) -> if y < 4000000 then Some(y,(y,(x+y))) else None) (1,1) |> Seq.filter (fun x -> x % 2 = 0) |> Seq.sum;;
3.
{1..600851475143.0 |> sqrt |> int}
|> Seq.filter (fun x -> {2..x |> float |> sqrt |> int} |> Seq.forall(fun y -> x = 2 || x % y > 0))
|> Seq.filter (int64 >> (%) 600851475143L >> (=) 0L)
|> Seq.last
4.
Seq.unfold (function | x,y when y = 100 -> Some(x*y, (x - 1, 999)) | x,y when x > 99 -> Some(x*y, (x, y - 1)) | _ -> None) (999,999) |> Seq.filter(fun x -> x.ToString() = System.String(Array.rev(x.ToString().ToCharArray()))) |> Seq.max;;
5.
{1..System.Int32.MaxValue} |> Seq.find (fun x -> {1..20} |> Seq.forall (fun y -> x % y = 0));;
6.
({1..100} |> Seq.sum |> fun x -> x * x) - ({1..100} |> Seq.map (fun x -> x * x) |> Seq.sum);;
7.
{2..System.Int32.MaxValue} |> Seq.filter (fun x -> {2..int(sqrt(float x))} |> Seq.forall(fun y -> x = 2 || x % y > 0)) |> Seq.take 10001 |> Seq.last;;
8.
"7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450".ToCharArray() |> Seq.map (fun x -> System.Int32.Parse(x.ToString())) |> Seq.windowed 5 |> Seq.map(fun x -> Seq.reduce (fun y z -> y * z) x) |> Seq.max;;
9.
Seq.unfold (function | x,y when y < 499 -> Some((x,y,1000 - y - x),(x, y + 1)) | x,y when y = 499 && x < 499 -> Some((x,y,1000 - y - x), (x + 1, x + 2)) | _ -> None) (1,2) |> Seq.find (fun (x,y,z) -> (x * x) + (y * y) = (z * z)) |> fun (x,y,z) -> x * y * z;;
10.
{2..1999999} |> Seq.filter (fun x -> {2..int(sqrt(float x))} |> Seq.forall(fun y -> x = 2 || x % y > 0)) |> Seq.map int64 |> Seq.sum;;
45.
let isShape m d n = ((n * m + 1. |> sqrt) + 1.) / d |> fun n -> n = floor n
{(40756.)..System.Double.MaxValue} |> Seq.filter (isShape 8. 4.) |> Seq.filter (isShape 24. 6.) |> Seq.head |> printfn "%f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment