Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Perfect numbers

54 views
Skip to first unread message

Lore

unread,
Feb 13, 2007, 5:19:11 AM2/13/07
to
> Hello everybody. Nice group but almost dead last days.
>
> Can someboy help me creating a function which can check if a natural
> number is perfect or not?
>
> Note: Perfect numbers are those natural which are equal to the sum of its
> divisors (but the number), for instance: 6 = 3*2*1 = 3+2+1
>
> Any help will be appreciated. Thanks in advance.
>

By the way, this is the code I have written, but can't get it to work:

divisor(X,Y) :- X < Y,0 is (Y mod X).

% auxiliar function:
% X = number, Y = divisor sum accumulator, Z = current divisor
perf(X,1,1).
perf(X,T,D) :- perf(X,Y,Z),divisor(D,X),T is (Y+D).

% main function:
perfect(X) :- perf(X,X,X).

Can someone rewrite it?


Lore

unread,
Feb 13, 2007, 4:57:20 AM2/13/07
to

Lore

unread,
Feb 13, 2007, 8:04:05 AM2/13/07
to

Don't bother fixing it, I have just done it myself:

divisor(X,Y) :- A is X, B is Y, X < Y, 0 is (B mod A).

perf(X,1,1).
perf(X,Y,Z) :- A is X, B is (Y-Z), C is (Z-1),C > 0, divisor(C+1,A),
perf(A,B,C).
perf(X,Y,Z) :- A is X, B is Y, C is (Z-1), C > 0, not(divisor(C+1,A)),
perf(A,B,C).

% main funct:
perfecto(X) :- T is X, T > 0, T is truncate(T), perf(T,T,T).

I'll probably have new problems for tomorrow. I'm looking forward to get
some assistance from you.

See ya


0 new messages