## generate all perfect numbers until 100. ...

## generate all perfect numbers until 100. ...

(OP)

Someone asked how to calculate the perfect numbers until 100 .And know I don't find that person. But here it is

%generate all perfect number until 100...

%Create perfect numbers

%divisible(10,2).

divisible(X,Y):- N is Y*Y,N =< X,X mod Y =:= 0.

divisible(X,Y):- Y < X, Y1 is Y + 1, divisible(X,Y1).

%isprime([3],Z).

isprime([X|_],X):-Y is 2, X >1, \+divisible(X,Y).

isprime([_|T],Z):-isprime(T,Z).

%Calculate the power of one number

%ie power(2,10,R)

power(_,0,1):-!.

power(N,K,R):-K1 is K-1,

power(N,K1,R1),

R is R1*N.

%formula of perfect numbers 2^(p-1)*(2^p-1)

%ie calc(2,10,R)

calc(2,K,R):-power(2,K,X),R1 is X-1,

power(2,K-1,R2),

R is R1 * R2.

%using lists

%ie calc([2,3,4],R).

listperf([K|_],R):-calc(2,K,R).

listperf([_|T],Z):-listperf(T,Z).

%generate one list of N numbers.

%genList(10,L).

generateList(0,[]).

generateList(N,[X|Xs]):- N > 0,

X is N+1,

N1 is N-1,generateList(N1,Xs).

%list of N perfect numbers

%perfect(100,C)

perfect(N,C):-generateList(N,R),findall(L,isprime(R,L),P),listperf(P,C).

%generate all perfect number until 100...

%Create perfect numbers

%divisible(10,2).

divisible(X,Y):- N is Y*Y,N =< X,X mod Y =:= 0.

divisible(X,Y):- Y < X, Y1 is Y + 1, divisible(X,Y1).

%isprime([3],Z).

isprime([X|_],X):-Y is 2, X >1, \+divisible(X,Y).

isprime([_|T],Z):-isprime(T,Z).

%Calculate the power of one number

%ie power(2,10,R)

power(_,0,1):-!.

power(N,K,R):-K1 is K-1,

power(N,K1,R1),

R is R1*N.

%formula of perfect numbers 2^(p-1)*(2^p-1)

%ie calc(2,10,R)

calc(2,K,R):-power(2,K,X),R1 is X-1,

power(2,K-1,R2),

R is R1 * R2.

%using lists

%ie calc([2,3,4],R).

listperf([K|_],R):-calc(2,K,R).

listperf([_|T],Z):-listperf(T,Z).

%generate one list of N numbers.

%genList(10,L).

generateList(0,[]).

generateList(N,[X|Xs]):- N > 0,

X is N+1,

N1 is N-1,generateList(N1,Xs).

%list of N perfect numbers

%perfect(100,C)

perfect(N,C):-generateList(N,R),findall(L,isprime(R,L),P),listperf(P,C).

## RE: generate all perfect numbers until 100. ...

see here http://en.wikipedia.org/wiki/Perfect_number.

I think that the person who wanted to calculate perfect numbers until 100 was waiting for something like that :

## CODE

numlist(2, N, L1),

include(perfect, L1, L).

perfect(N) :-

divisors(N, 2, [1], L),

sumlist(L, N).

divisors(N, D, LC, LF) :-

0 is N mod D,

!,

D1 is D + 1,

X is N / D,

( X >= D -> divisors(N, D1, [X, D |LC], LF); LC = LF).

divisors(N, D, LC, LF) :-

X is N / D,

( X > D -> D1 is D + 1, divisors(N, D1, LC, LF); LF = LC).

## RE: generate all perfect numbers until 100. ...

yes

I have one error on the first part of this formula

I need to check, not only that p is prime,but that 2^p-1 is also prime(first part of the formula).

## RE: generate all perfect numbers until 100. ...

This program need to improve performance with a prime numbers test faster.

%----------------

%Find perfect numbers

hasFactor(_,0):-!,fail.

hasFactor(N,L):-L<N,N mod L=:=0.

hasFactor(N,L):-L<N,L2 is L+1,hasFactor(N,L2).

%test of prime number

%isprime([3],Z).

isprime([X|_],X):-Y is 2, X >1, \+hasFactor(X,Y).

isprime([_|T],Z):-isprime(T,Z).

%multiply with sums

%product of natural numbers with successives sums:

% |0 se y=0

% mul(x,y)|

% |x+mul(x,y-1) se y>0

mul(_,0,0):-!.

mul(X,Y,Z):-X>0,Y>0,

Y1 is Y-1,

mul(X,Y1,Z1),Z is X+Z1.

%Calculate the power of one number

%ie power(2,10,R)

power(_,0,1):-!.

power(N,K,R):-K1 is K-1,

power(N,K1,R1),

mul(R1,N,R).

%generate one list of N numbers.

%genList(10,L).

generateList(0,[]).

generateList(N,[X|Xs]):- N > 0,

X is N+1,

N1 is N-1,generateList(N1,Xs).

%formula of perfect numbers (2^p-1)*2^(p-1)

%ie calc(2,10,R)

calc(2,K,R):-power(2,K,X),R1 is X-1,primo(R1),

power(2,K-1,R2),

mul(R1,R2,R).

primo(X):-Y is 2, X >1, \+hasFactor(X,Y).

%output the list

noLst([X|_],X).

noLst([_|T],Z):-noLst(T,Z).

%calculate perfect numbers

%ie perfect(12,N).

perfect(N,R):-generateList(N,Xs),noLst(Xs,K),calc(2,K,R).

## RE: generate all perfect numbers until 100. ...

## CODE

N mod L=:=0.

hasFactor(N,L):-

L<sqrt(N),

L2 is L+1,

hasFactor(N,L2).