/* Digital root (Rosetta Code) in Picat. http://rosettacode.org/wiki/Digital_root """ The digital root (X) of a number (N) is calculated: - find X as the sum of the digits of N - find a new X by summing the digits of X repeating until X has only one digit. The additive persistence is the number of summations required to obtain the single digit. The task is to calculate the additive persistence and the digital root of a number. e.g. 627615 has additive persistence 2 and digital root of 9; 39390 has additive persistence 2 and digital root of 6; 588225 has additive persistence 2 and digital root of 3; 393900588225 has additive persistence 2 and digital root of 9; The digital root may be calculated in bases other than 10. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => foreach(N in [627615,39390,588225,393900588225, 58142718981673030403681039458302204471300738980834668522257090844071443085937]) [Sum,Persistence] = digital_root(N), printf("%w har addititive persistence %d and digital root of %d\n", N,Persistence,Sum) end, nl. % % (Reduced) digit sum (digital root) of a number % digital_root(N) = [Sum,Persistence], integer(N) => Sum = N, Persistence = 0, while(Sum > 9) Sum := sum([I.to_integer() : I in Sum.to_string()]), Persistence := Persistence + 1 end.