How can I define in Prolog the rule to calculate the digit sum of an integer?
Example:
digitsum[Integer, Result] :- ...
so that the query digitsum(123, X).
results in X = 6
(=1+2+3). A solution running under SWI-Prolog is appreciated.
How can I define in Prolog the rule to calculate the digit sum of an integer?
Example:
digitsum[Integer, Result] :- ...
so that the query digitsum(123, X).
results in X = 6
(=1+2+3). A solution running under SWI-Prolog is appreciated.
Copyright © 2021 Jogjafile Inc.
This problem naturally leads to an implementation using
library(clpfd)
:The first answer is about zero. The second says that a number 1..9 is the same as its sum. Then it talks about the numbers 10..99 etc.