Find the Factorial of a number predicates go fact(integer,integer) clauses go:- write('give the no:'), readint(X), Z=1, fact(X,Z). Fact(1,Z):- write('the. May 09, 2017 Prolog factorial non-recursive. Ask Question -1. How would you convert this to non-recursive. Writing a factorial-like function (Prolog) 0. Non recursive factorial in C. Recursive procedure explanation. Understanding factorial recursion. Walking through the recursive factorial program in Prolog? Sums up the values of all.
Active1 year, 2 months ago
I'm having trouble understanding the following factorial program
When
CyberShotCyberShotfact1
is called nested within the second fact1
, doesn't that mean that the the last line, Result is Result1*N.
, is never called? Or in Prolog does the last line get executed before the recursive call?9975 gold badges16 silver badges32 bronze badges
9 Answers
No, the recursive call happens first! It has to, or else that last clause is meaningless. The algorithm breaks down to:
As you can see, you need to calculate the result of the recursion before multiplying in order to return a correct value!
Your prolog implementation probably has a way to enable tracing, which would let you see the whole algorithm running. That might help you out.
weston40.7k17 gold badges104 silver badges177 bronze badges
Carl NorumCarl Norum182k22 gold badges359 silver badges433 bronze badges
BTW once you got the basic recursion understood, try to achieve tail recursion whenever possible, here it'd be:
Tail recursion, unlike the recursion you used previously, allows interpreter/compiler to flush context when going on to the next step of recursion. So let's say you calculate
factorial(1000)
, your version will maintain 1000 contexts while mine will only maintain 1. That means that your version will eventually not calculate the desired result but just crash on an Out of call stack memory
error.You can read more about it on wikipedia.
m09m096,2572 gold badges25 silver badges57 bronze badges
Mohamed HassanMohamed Hassan
Generally speaking, @m09's answer is basically right about the importance of tail-recursion.
For big
N
, calculating the product differently wins! Think 'binary tree', not 'linear list'...Let's try both ways and compare the runtimes. First, @m09's
factorial/2
:Next, we do it tree-style—using meta-predicate
reduce/3
together with lambda expressions:Last, let's define and use dedicated auxiliary predicate
x_y_product/3
:What's to gain? Let's ask the stopwatch!
Community♦
repeatrepeat15.3k4 gold badges44 silver badges139 bronze badges
Floern28.2k15 gold badges90 silver badges108 bronze badges
Salma Salma
Base case is declared. The conditions that N must be positive and multiply with previous term.
To run:
factorial(-1,X).
Shashank MotepalliShashank Motepalli
rashedcsrashedcs
I would do something like:
And a tail version would be like:
So for you to call the:
non-tail recursive method: fact(3, Result).
tail recursive method: tail_fact(3, 1, Result).
This might help ;)
João R.João R.
Peter OsamaPeter Osama
Not the answer you're looking for? Browse other questions tagged recursionprologfactorial or ask your own question.
Active2 years, 4 months ago
How would you convert this to non-recursive. This code puts out the factorial of N.
false10k9 gold badges76 silver badges155 bronze badges
twin92twin92
2 Answers
One way to do it without a recursive call in your definition would be:
This approach avoids recursion on syntactic level in your definition. Both
numlist/2
and foldl/4
would most probably have a recursive definition, but you don't have to look at it. This probably falls into the 'd) something else I am missing' category from my comment to your question.Community♦
user1812457
if your Prolog has global variables, you can do
This particular usage of nb_setval/2, nb_getval/2 could be partially simulated with assert/1, retract/1, but the resulting program would be very, very inefficient
CapelliCCapelliC52.9k4 gold badges34 silver badges66 bronze badges