Skip to content

Instantly share code, notes, and snippets.

@chomado
Created May 26, 2013 18:23
Show Gist options
  • Save chomado/5653596 to your computer and use it in GitHub Desktop.
Save chomado/5653596 to your computer and use it in GitHub Desktop.
n 以下の完全数をリストで返す
(* 完全数: 自分自身を除く約数の和がその数自身になる *)
(* n以下の整数一覧を並べたリスト *)
(* enumerate: int -> int list *)
let rec enumerate n = if n <= 0 then [] else n :: enumerate (n-1)
(* n 以下の完全数をリストで返す *)
(* perfect : int -> int list *)
let perfect n =
let perfectp n =
List.fold_right (+) (List.filter (fun a -> n mod a = 0) (enumerate n)) 0
= 2 * n
in List.filter perfectp (enumerate n)
(* テスト *)
let test1 = perfectp 6 = true
let test2 = perfect 1000 = [496; 28; 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment