Gaffaweb > Love & Anger > 1995-06 > [ Date Index | Thread Index ]
[Date Prev] [Date Next] [Thread Prev] [Thread Next]


Brit Awards

From: smith david rt <p0070421@brookes.ac.uk>
Date: Tue, 21 Feb 1995 19:27:36 +0000
Subject: Brit Awards
To: Love-Hounds@uunet.UU.NET
Content-Length: 1173

Hello Lau,

I'm in your group for the prolog programming coursework on 8044 Logic Programming. 

Sorry I couldn't make it to our meeting yesterday (Monday 20th 6.00pm) I was
off sick. I've managed to get a program together that prints out all perfect
numbers up to about 6000. I've included the program below.

See you on Thursday anyway.

David

findp :-
	Num is 1,
	findperfects(Num).
findperfects (Num) :-
	not( perfect(Num)),
	NewNum is Num + 1,	
	findperfects(NewNum). 
	
	
findperfects (Num) :-
	perfect(Num),
	write(Num), nl,
	NewNum is Num + 1,	
	findperfects(NewNum). 


perfect(Limit) :-
	sum(Limit,X),
	!,
	Limit = X.

sum(Limit,X) :-
	factors(Limit, Ps),
	sumlist(Ps,X).

sumlist([],0).

sumlist([First|Rest],Sum) :-
        sumlist(Rest,SumRest),
        Sum is First + SumRest.



factors( Limit, Ps ) :- 
	integers( 1, Limit, Is ), 
	sift( Limit, Is, Ps ).

integers( Low, High, [Low|Rest] ) :-
   	Low < High, 
	M is Low+1, 
	integers(M, High, Rest ).

integers( _,_,[] ).

sift( Limit, [], [] ).

sift( Limit, [I|Is], [I|Ps]) :- 
	0 is Limit mod I,
	sift( Limit, Is, Ps ).

sift( Limit, [I|Is], Ps ) :-
	not ( 0 is Limit mod I),
	sift( Limit, Is, Ps ).