Can’t resist, here’s a solution in Reason: (Try it)
let rec digits = (n) => n === 0 ? [] : [n mod 10, ...digits(n / 10)]; let squaresSum = (digits) => digits |> List.map((n) => n * n) |> List.fold_left((a, b) => a + b, 0); let rec isHappy = (n) => switch n { | 1 => true | 4 => false | _ => isHappy(n |> digits |> squaresSum) }; let rec nextHappyNumber = (n) => isHappy(n) ? n : nextHappyNumber(n + 1); let firstHappyNumbers = (nb) => { let rec firstHappyNumbersFrom = (nb, found) => if (nb === 0) { found } else { let from = List.length(found) === 0 ? 1 : List.hd(found) + 1; let next = nextHappyNumber(from); firstHappyNumbersFrom(nb - 1, [next, ...found]) }; firstHappyNumbersFrom(nb, []) }; Js.log(firstHappyNumbers(20));
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Can’t resist, here’s a solution in Reason: (Try it)