// Souffle - A Datalog Compiler
// Copyright (c) 2016, The Souffle Developers. All rights reserved
// Licensed under the Universal Permissive License v 1.0 as shown at:
// - https://opensource.org/licenses/UPL
// - <souffle root>/licenses/SOUFFLE-UPL.txt

//
// Find the perfect numbers that occur before the limit
//

.decl gen(x: number)
gen(1).
gen(x+1) :- gen(x), !lim(x).

.decl lim(x : number)
lim(100).

//fc is factors of x, excluding itself
.decl e_factor(x : number, fc : number)
e_factor(x, fc) :- gen(fc), gen(x), x % fc = 0, x!=fc.

//x is equal to the sum of its factors excluding itself
.decl perfect(x : number)
.output perfect()
perfect(x) :- gen(x), x = sum inv : e_factor(x, inv).
