Created
March 26, 2024 06:49
-
-
Save narslan/24fc00c7ed53693a90d85d76d96841f6 to your computer and use it in GitHub Desktop.
Revisions
-
narslan created this gist
Mar 26, 2024 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ (*This implementation is slow. It can be improved. The summings function can use memoizing. *) fun divisors n = (List.filter (fn x => n mod x = 0) (List.tabulate (n div 2, fn x => x + 1 ))) fun powerset [] = [] | powerset [x] = [[],[x]] | powerset (x::xs) = let val power_subset = powerset xs in (List.map (fn L => x::L) power_subset) @ power_subset end fun summings p = List.map (fn l => foldl (fn (x,y) => x + y ) 0 l ) (powerset (divisors p)) ; fun practical p = List.all (fn n => List.exists (fn x => n = x ) (summings p)) (List.tabulate(p , fn x => x + 1))