Skip to content

Instantly share code, notes, and snippets.

View theburningmonk's full-sized avatar

Yan Cui theburningmonk

View GitHub Profile
@theburningmonk
theburningmonk / gist:1201982
Created September 7, 2011 22:24
ProjectEuler - Problem 40 Solution
// the base sequence of natural numbers
let naturalNumbers = Seq.unfold (fun x -> Some(x, x+1)) 1
// the decimal fraction 12345678910111213 as a sequence of strings
let fraction =
naturalNumbers
|> Seq.collect (fun x -> x.ToString().ToCharArray() |> Seq.map (fun c -> c.ToString()))
// define function d
let d n = int(fraction |> Seq.nth (n-1))
@theburningmonk
theburningmonk / gist:1202054
Created September 7, 2011 22:54
ProjectEuler - Problem 55 Solution
open System.Linq
// checks if the number n is palindromic
let isPalindromic n =
let charArray = n.ToString().ToCharArray()
let revCharArray = Array.rev charArray
charArray.SequenceEqual(revCharArray)
// reverse a number, e.g. 1234 -> 4321
let reverse n =
@theburningmonk
theburningmonk / gist:1202056
Created September 7, 2011 22:57
ProjectEuler - Problem 56 Solution
let digitalSum n = n.ToString().ToCharArray() |> Array.map (fun c -> int(c.ToString())) |> Array.sum
let answer =
[1I..100I]
|> List.collect (fun a -> [1..100] |> List.map (fun b -> digitalSum(pown a b)))
|> List.max
@theburningmonk
theburningmonk / gist:1203911
Created September 8, 2011 16:58
ProjectEuler - Problem 74 Solution
open System.Collections.Generic
// factorial function
let factorial n = if n = 0 then 1 else [1..n] |> List.reduce (fun acc x -> acc * x)
// create a cache of sorts to help improve performance and prepopulate them
let factorials, terms = new Dictionary<int, int>(), new Dictionary<int, int>()
terms.Add(169, 3)
terms.Add(871, 2)
terms.Add(872, 2)
@theburningmonk
theburningmonk / gist:1207599
Created September 9, 2011 23:30
ProjectEuler - Problem 145 Solution
let digitBases = [9..-1..0] |> List.map (fun n -> pown 10 n)
/// turns the specified number into a list of its digits
let toDigits (n : int) =
digitBases |> List.filter (fun n' -> n > n') |> List.map (fun n' -> (n / n') % 10)
/// turns the specified list of digits into the number they represent
let toNumber (digits : int list) =
digits |> List.mapi (fun i n -> n * pown 10 (digits.Length - i - 1)) |> List.sum
@theburningmonk
theburningmonk / gist:1209500
Created September 11, 2011 12:07
ProjectEuler - Problem 79 Solution
open System.IO
open System.Collections.Generic
// read the keylog file into a int [] []
let entries = File.ReadAllLines(@"c:\temp\keylog.txt")
|> Array.map (fun str -> str.ToCharArray() |> Array.map (fun c -> int(c.ToString())))
// dictionary for keeping track of the list of numbers that falls behind a number
let inFrontOf = new Dictionary<int, int list>();
@theburningmonk
theburningmonk / gist:1209765
Created September 11, 2011 16:09
ProjectEuler - Problem 59 Solution
open System
open System.IO
// the top 10 most common words in English
let mostCommmonWords = ["the"; "be"; "to"; "of"; "and"; "a"; "in"; "that"; "have"; "I"]
// read the encrypted ASCII codes
let cipherBytes = File.ReadAllText(@"c:\temp\cipher1.txt").Split(',')
|> Array.map (fun str -> byte(str.Trim()))
@theburningmonk
theburningmonk / gist:1247782
Created September 28, 2011 12:10
Ayande's tax challenge
let taxRates =
[((0.0, Some(5070.0)), 0.1);
((5070.0, Some(8660.0)), 0.14);
((8660.0, Some(14070.0)), 0.23);
((14070.0, Some(21240.0)), 0.3);
((21240.0, Some(40230.0)), 0.33);
((40230.0, None), 0.45)]
let calcTaxRate (n : float) =
List.fold (fun acc ((lower, upper), rate) ->
@theburningmonk
theburningmonk / gist:1249627
Created September 28, 2011 23:57
Contrived C# example
// dummy method that throws an exception based on the input
public void ThrowException(int n) {
switch (n) {
case 0:
throw new Exception("Default exception");
case 1:
throw new ArgumentException("Invalid argument");
case 2:
throw new ArgumentNullException();
case 3:
@theburningmonk
theburningmonk / gist:1249638
Created September 29, 2011 00:03
Contrived F# example
open System
let throwException =
function
| 0 -> failwithf "Default exception"
| 1 -> raise <| new ArgumentException("Invalid N")
| 2 -> raise <| new ArgumentNullException()
| 3 | 4 | 5 -> raise <| new ArgumentOutOfRangeException()
| _ -> raise <| new ApplicationException()