summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--day19.fsx15
1 files changed, 15 insertions, 0 deletions
diff --git a/day19.fsx b/day19.fsx
new file mode 100644
index 0000000..f30c140
--- /dev/null
+++ b/day19.fsx
@@ -0,0 +1,15 @@
+open System
+
+let rec josephus = 
+    function
+        | 1 -> 1
+        | x  -> (if x % 2 <> 0 then 1 else -1) + 2 * josephus (x / 2)
+
+let josephus' (target: int): int =
+    let pow = Math.Pow(3, Math.Floor(Math.Log(target, 3))) |> int
+    if pow = target then pow
+    else if pow >= target / 2 then target - pow
+    else pow + (2 * (target - 2 * pow))
+
+josephus 3017957 |> printfn "%d"
+josephus' 3017957  |> printfn "%d"
\ No newline at end of file