Skip to content

Instantly share code, notes, and snippets.

@narslan
Created March 26, 2024 06:49
Show Gist options
  • Save narslan/24fc00c7ed53693a90d85d76d96841f6 to your computer and use it in GitHub Desktop.
Save narslan/24fc00c7ed53693a90d85d76d96841f6 to your computer and use it in GitHub Desktop.

Revisions

  1. narslan created this gist Mar 26, 2024.
    15 changes: 15 additions & 0 deletions gistfile1.txt
    Original 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))