number of digits in integer number using prolog

huangapple go评论56阅读模式
英文:

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 ?

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).

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需要两个参数。

  1. 转换为字符串并测量字符串。这在Python中是一种作弊,但Prolog甚至不需要,它可以直接告诉你:
?- atom_length(100, Len).
Len = 3
  1. 啊,这个递归循环版本,你应该努力解决,这样你可以学到递归的知识:

对于基本版本:

% 一个单独的数字长度为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).

通过以下方式进行改进:

  • 使用清晰的变量名称
  • 使其成为尾递归,以使其运行更高效
  • 使其确定性,以消除选择点
  1. 放弃所有这些,并通过以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.

  1. 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
  1. 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 &gt;= 0,
    N =&lt; 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 &gt; 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
  1. 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 &gt; 9,                        % Is the value &gt; 9?
    !,                            %   If so, eliminate the choice point (it&#39;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&#39;s left.
    .                             % Otherwise (the value is 0-9)
number_of_digits( _ , N , N ) .   %   The accumulator has the result.

huangapple
  • 本文由 发表于 2023年1月6日 11:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定