This file contains hidden or 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
// 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)) |
This file contains hidden or 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.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 = |
This file contains hidden or 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 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 |
This file contains hidden or 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.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) |
This file contains hidden or 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 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 |
This file contains hidden or 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.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>(); |
This file contains hidden or 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.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())) |
This file contains hidden or 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 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) -> |
This file contains hidden or 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
// 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: |
This file contains hidden or 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 | |
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() |