-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(p19): add lazy collection implementation
- Loading branch information
Showing
3 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
(library | ||
(name p12_lib) | ||
(modules iterative map module rec tailrec)) | ||
(modules iterative map module rec tailrec lazy)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
let triangular_numbers = | ||
Seq.unfold | ||
(fun n -> | ||
let t = n * (n + 1) / 2 in | ||
Some (t, n + 1)) | ||
1 | ||
;; | ||
|
||
let count_factors_tail n = | ||
let rec aux i count = | ||
if i * i > n | ||
then count | ||
else if n mod i = 0 | ||
then aux (i + 1) (count + if i * i = n then 1 else 2) | ||
else aux (i + 1) count | ||
in | ||
if n <= 0 then 0 else aux 1 0 | ||
;; | ||
|
||
let find_first_triangular_with_factors n = | ||
Seq.find (fun x -> count_factors_tail x > n) triangular_numbers | ||
;; | ||
|
||
let result = find_first_triangular_with_factors 500 |
This file contains 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