prolog - generation of positive numbers in a special case -


I'm looking for the possibility to change this code

  zero (0) Positive (x): - \ + Zero (x)  

so that's the call

 ? - Positive (x).  

will generate values ​​for X.

Actually calls for specific values ​​only for X are checked, but no value can be generated.

Thank you.

You can literally write

  positive (1). Positive (x): - positive (y) is x y + 1 And it will really work  
 ? - Positive (x). X = 1; X = 2; X = 3; X = 4; Etc.  

But keep in mind that this algorithm is very complex due to not being tail-recursive. I mean:

 ? - Time ((positive (x), x> 4000)). % 8,010,001 intenrences, 3.7 seconds in 3.70 seconds (99% CPU, 2163038 lip) X = 4001.  

Pay attention to the time for building 4000 positive.

But if we like it:

  Positive 1 (x): - to (1, x). (From, from) From (to, x): - from 1 to 1 + 1, from (1, x).  

All work now faster:

 ? - Time ((positive 1 (x), x> 4000)). % 8,002 Innerces, 0.00 secs in CPU (?% CPU, Eternal Lips) X = 4001.  

Comments