diff --git a/gleam/prime-factors/src/prime_factors.gleam b/gleam/prime-factors/src/prime_factors.gleam index a6412af..b456f48 100644 --- a/gleam/prime-factors/src/prime_factors.gleam +++ b/gleam/prime-factors/src/prime_factors.gleam @@ -1,13 +1,13 @@ import gleam/list pub fn factors(value: Int) -> List(Int) { - do_factors(value, 2, []) + list.reverse(factors_iter(value, 2, [])) } -fn do_factors(n: Int, i: Int, factors: List(Int)) -> List(Int) { - case i <= n, n % i == 0 { - False, _ -> factors - _, True -> do_factors(n / i, i, list.append(factors, [i])) - _, False -> do_factors(n, i + 1, factors) +fn factors_iter(n: Int, i: Int, factors: List(Int)) -> List(Int) { + case n, n % i { + 1, _ -> factors + _, 0 -> factors_iter(n / i, i, [i, ..factors]) + _, _ -> factors_iter(n, i + 1, factors) } }