Prolog,给定N并找到所有不能被3和5整除的数字,这些数字必须小于N

编程入门 行业动态 更新时间:2024-10-23 23:27:06
本文介绍了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

本文发布于:2023-11-30 21:05:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   Prolog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!