Files
project-euler/26.fs
T
2025-08-03 20:16:55 -07:00

30 lines
931 B
FSharp

module _26
let division d =
let digit = 10 / d
let mutable remainder = 10 % d
let mutable quotient = [(digit, remainder)]
let mutable cycleIndex = None
while remainder <> 0 && cycleIndex = None do
remainder <- remainder * 10
let digit = remainder / d
remainder <- remainder % d
let next = (digit, remainder)
let cycle = quotient |> List.tryFind (fun e -> e = next)
if cycle <> None then
cycleIndex <- Some(
quotient.Length
- (quotient |> List.findIndex (fun e -> e = next))
- 1)
else quotient <- next :: quotient
List.rev quotient |> List.map fst, cycleIndex
let cycleLength d =
match division d with
| (_,None) -> 0
| (q,Some(c)) -> q.Length - c
let longestRecurringCycle =
[2..1000]
|> List.mapi ( fun i d -> (i + 2, cycleLength d))
|> List.maxBy snd