Prolog,给定N并找到所有不能被3和5整除的数字,这些数字必须小于N [英] Prolog, given N and find all numbers not divisible by 3 and 5 and these numbers must be smaller than N

查看:138
本文介绍了Prolog,给定N并找到所有不能被3和5整除的数字,这些数字必须小于N的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到解决问题的方法。

Divisible / 2 谓词检查数字N是否可被列表中的数字之一整除

I have problem with find a solution to the problem.
Divisible/2 predicate examines whether a number N is divisible by one of numbers in the list

divisible([H|_],N) :- N mod H =:= 0.
divisible([H|T],N) :- N mod H =\= 0, divisible(T,N).

我需要建立一个谓词查找,该查找将找到Number< N不能被数字列表整除的

示例输入/输出:

I need to build a predicate find that will find Number < N that are not divisible by the list of numbers
example input/output:

?- find(5, [3,5],Num).
output is : 
Num = 4; Num = 2; Num = 1. False
Here N is 5 and list of number is [3,5]

当前代码:

findNum(1, LN, Num) :- \+ divisible(LN,1),
                        Num is 1.

findNum(Rank, LN, Num) :- Rank > 1,
                        Num1 is Rank - 1,
                        ( \+ divisible(LN,Num1) -> Num is Num1;
                        findNum(Num1,LN, Num) ).

仅打印Num = 4;出于某些原因,它永远不会打印2和1。

而且我不确定哪里出了问题。.
可以提供任何帮助...

It only prints Num = 4; It never prints 2 and 1 for some reasons
And I am not sure where goes wrong.. Any help is appreciated...

推荐答案

尝试将findNum谓词修改为:

Try to modify the findNum predicate into:

findNum(Rank, LN, Num) :- Rank > 1,
                          Num1 is Rank - 1,
                          \+ divisible(LN,Num1) -> Num is Num1.
findNum(Rank, LN, Num) :-  Rank > 1, 
                           Num1 is Rank - 1, 
                           findNum(Num1,LN, Num).

对我来说,它给出了所需的答案。

For me, it gives the requested answer.

这篇关于Prolog,给定N并找到所有不能被3和5整除的数字,这些数字必须小于N的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆