英文:
number of digits in integer number using prolog
问题
可以有人帮我解决这个问题吗?
numofdig(A,B):-
A>0,
A is (A//10),
succ(0,S),
numofdig(A),
B is S,
write(B).
这是我的代码,但它不起作用。
英文:
can someone help me with this problem ?
numofdig(A,B):-
A>0,
A is (A//10),
succ(0,S),
numofdig(A),
B is S,
write(B).
this is my code and its not working .
答案1
得分: 0
SWISH(你图中的网站)的查询框左侧已经有?-
,你无需输入它。在查询中输入它会导致错误procedure (?-A) does not exist
。
succ(0, S)
将始终是S=1
,而B is S
意味着B始终是1,这是不正确的。
numofdig(A)
不可能是正确的,因为numofdig
需要两个参数。
- 转换为字符串并测量字符串。这在Python中是一种作弊,但Prolog甚至不需要,它可以直接告诉你:
?- atom_length(100, Len).
Len = 3
- 啊,这个递归循环版本,你应该努力解决,这样你可以学到递归的知识:
对于基本版本:
% 一个单独的数字长度为1
num_dig(N, 1) :-
N >= 0,
N =< 9.
% 对于其他数字,除以10使其变短1位,
% 获取那个长度(递归到单个数字),
% 在返回过程中为每个较短的长度添加1。
num_dig(N, C) :-
N > 9,
A is N // 10,
num_dig(A, B),
succ(B, C).
通过以下方式进行改进:
- 使用清晰的变量名称
- 使其成为尾递归,以使其运行更高效
- 使其确定性,以消除选择点
- 放弃所有这些,并通过以10为底的对数来计算数字的位数:
num_dig(N, Len) :-
Len is ceil(log10(N + 1)).
英文:
SWISH (the website in your image) already has ?-
to the left of the query box, you don't need to type it in. Typing it in your query is what causes the error procedure (?-A) does not exist
.
succ(0, S)
will always be S=1
, and B is S
means B is always 1, that's not right.
numofdig(A)
can't be right because numofdig takes two parameters.
- Convert to string and measure the string. That's a cheat for Python, but Prolog doesn't even need that it can tell you directly:
?- atom_length(100, Len).
Len = 3
- Ugh this recursive loop version, which you are supposed to struggle through so you can learn about recursion:
For a basic version:
% a single digit is length 1
num_dig(N, 1) :-
N >= 0,
N =< 9.
% other numbers, divide by 10 to shrink it 1 shorter,
% get the length of that (recursion down to single digit),
% add 1 to each shorter length on the way back up.
num_dig(N, C) :-
N > 9,
A is N // 10,
num_dig(A, B),
succ(B, C).
Improve it by:
- giving the variables clear names
- making it tail recursive so it can run more efficiently
- making it deterministic so it leaves no choicepoints
- Scrap all that and calculate the number of digits from the base 10 log of the number:
num_dig(N, Len) :-
Len is ceil(log10(N + 1)).
答案2
得分: 0
我会这样做:
number_of_digits( M , N ) :-
integer(M), % 它是整数吗?
V is abs(M), % 符号与数字的位数无关
number_of_digits(V,1,N). % 使用初始值1(所有数字至少有一位数)调用辅助谓词。
number_of_digits( M , T , N ) :-
M > 9, % 值是否大于9?
!, % 如果是,消除选择点(回溯时它不会停止为10+)
T1 is T+1 , % 增加累加器的值
M1 is M // 10 , % 通过整数除法除去最右边的数字
number_of_digits(M1,T1,N) % 并对剩下的部分进行递归。
. % 否则(值为0-9)
number_of_digits( _ , N , N ) . % 累加器包含结果。
英文:
I'd do this:
number_of_digits( M , N ) :-
integer(M), % is it an integer?
V is abs(M), % sign is irrelevant as to number of digits
number_of_digits(V,1,N). % invoke the helper predicate with the accumulator seeded with 1 (all numbers have at least one digit).
number_of_digits( M , T , N ) :-
M > 9, % Is the value > 9?
!, % If so, eliminate the choice point (it's not going to stop being 10+ on backtracking)
T1 is T+1 , % Increment the accumulator
M1 is M // 10 , % drop the rightmost digit via integer division by 10
number_of_digits(M1,T1,N) % and recurse down on what's left.
. % Otherwise (the value is 0-9)
number_of_digits( _ , N , N ) . % The accumulator has the result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论