0

The pyDatalog page shows how to implement the factorial algorithm to calculate N! values. Is it possible to modify it (eg. using predicates) to solve for which N the N! will equal a given value (eg. 6) ?

from pyDatalog import pyDatalog
pyDatalog.create_terms('factorial, N')

factorial[N] = N*factorial[N-1] 
factorial[1] = 1
print(factorial[3]==N)  # prints N=6

But I would like to ask:

print(factorial[N]==6)  # and receive N=3

Unfortunately is such case pyDatalog says:

DatalogError: Error: left hand side of comparison must be bound: >/2
aoi
  • 11
  • 2
  • 2
    Basically what you're looking for is an "inverse factorial" function which I do not believe has a closed-form solution. So the quick answer to your question is "no". Probably your best bet is to just define another function which contains a table of hardcoded inverse factorial values and it just looks up the right one based on the input. Otherwise you may be leading down a path of some extremely intense maths. – ImaginaryHuman072889 Nov 22 '17 at 18:14
  • Thank you for the answer. I have an impression that "inverse factorial" [can be solved in Prolog](https://stackoverflow.com/questions/19025664/inverse-factorial-in-prolog). So let me confirm - is the "inverse factorial" the example of generally stronger limitations of Datalog in comparison to Prolog? – aoi Nov 22 '17 at 18:41

0 Answers0