Skip to content

Instantly share code, notes, and snippets.

@frectonz
Last active May 8, 2023 09:55
Show Gist options
  • Save frectonz/e96e43adbd328123010687e230b9f566 to your computer and use it in GitHub Desktop.
Save frectonz/e96e43adbd328123010687e230b9f566 to your computer and use it in GitHub Desktop.
From cassido's May 05, 2023 Newsletter
let int_sqrt x = x |> float_of_int |> sqrt |> int_of_float
(**
* [oddSquareNumber n]
* returns the sum of all odd square numbers less than [n]
*
* it does this by going through all the odd numbers from [1] to [sqrt(n)]
* and adding the square of each number to the sum if the square is less
* than [n].
*)
let oddSquareSum n =
let rec inner max curr sum =
if curr > max then sum
else
let curr_square = curr * curr in
let sum = sum + if curr_square < n then curr_square else 0 in
inner max (curr + 2) sum
in
inner (int_sqrt n) 1 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment