I’m currently trying to submit my proposed solution to the Perfect Numbers exercise in F#.
Until now, I was not able to make the automated tests finish.
My code submission:
module PerfectNumbers
type Classification =
| Perfect
| Abundant
| Deficient
let rec getAliquotSum (number: int) (state: int) (out: int list): int =
if (state = number) then out |> List.sum
else
match (number % state) with
| 0 -> getAliquotSum number (state + 1) (state::out)
| _ -> getAliquotSum number (state + 1) out
let classify (number: int): Classification option =
if (number < 1) then None
else
let aliquotSum = getAliquotSum number 2 [1]
if (aliquotSum = number) then Some Classification.Perfect
elif (aliquotSum > number) then Some Classification.Abundant
else Some Classification.Deficient
The automated tests error:
YOUR TESTS TIMED OUT
Your tests timed out. This might mean that there was an issue in our infrastructure, but more likely it suggests that your code is running slowly. Is there an infinite loop or something similar?
Please check your code, and if nothing seems to be wrong, try running the tests again.
I have tested the algorithm locally with different inputs and all of them worked fine.
// Perfect:
classify 6
classify 28
// Abundant:
classify 12
classify 24
// Deficient:
classify 8
classify 13
// Wrong input:
classify 0
classify -1