Prevent backtracking after first solution to Fibonacci pair

前端 未结 3 1724
感情败类
感情败类 2021-01-01 00:47

The term fib(N,F) is true when F is the Nth Fibonacci number.

The following Prolog code is generally working for me:



        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-01 01:30

    If you are only interested in the first solution or know that there is at most one solution, you can use once/1 to commit to that solution:

    ?- once(fib(X, 377)).

    +1 for using CLP(FD) as a declarative alternative to lower-level arithmetic. Your version can be used in all directions, whereas a version based on primitive integer arithmetic cannot.

提交回复
热议问题