R – Finding perfect numbers between 1 and 100

perfect-numbersprolog

How can I generate all perfect numbers between 1 and 100?

A perfect number is a positive integer that is equal to the sum of its proper divisors. For example, 6(=1+2+3) is a perfect number.

Best Answer

So I suspect Frank is looking for an answer in Prolog, and yes it does smell rather homeworky...

For fun I decided to write up my answer. It took me about 50 lines.

So here is the outline of what my predicates look like. Maybe it will help get you thinking the Prolog way.

  is_divisor(+Num,+Factor)

  divisors(+Num,-Factors)
  divisors(+Num,+N,-Factors)

  sum(+List,-Total)
  sum(+List,+Sofar,-Total)

  is_perfect(+N)

  perfect(+N,-List)

The + and - are not really part of the parameter names. They are a documentation clue about what the author expects to be instantiated.(NB) "+Foo" means you expect Foo to have a value when the predicate is called. "-Foo" means you expect to Foo to be a variable when the predicate is called, and give it a value by the time it finishes. (kind of like input and output, if it helps to think that way)

Whenever you see a pair of predicates like sum/2 and sum/3, odds are the sum/2 one is like a wrapper to the sum/3 one which is doing something like an accumulator.

I didn't bother to make it print them out nicely. You can just query it directly in the Prolog command line:

?- perfect(100,L).
L = [28, 6] ;
fail.

Another thing that might be helpful, that I find with Prolog predicates, is that there are generally two kinds. One is one that simply checks if something is true. For this kind of predicate, you want everything else to fail. These don't tend to need to be recursive.

Others will want to go through a range (of numbers or a list) and always return a result, even if it is 0 or []. For these types of predicates you need to use recursion and think about your base case.

HTH.

NB: This is called "mode", and you can actually specify them and the compiler/interpreter will enforce them, but I personally just use them in documentation. Also tried to find a page with info about Prolog mode, but I can't find a good link. :(

Related Topic