Last active
May 8, 2023 09:55
-
-
Save frectonz/e96e43adbd328123010687e230b9f566 to your computer and use it in GitHub Desktop.
From cassido's May 05, 2023 Newsletter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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