/* Digital roots in Picat. http://en.wikipedia.org/wiki/Digital_root """ The digital root (also repeated digital sum) of a number is the number obtained by adding all the digits, then adding the digits of that number, and then continuing until a single-digit number is reached. For example, the digital root of 65,536 is 7, because 6 + 5 + 5 + 3 + 6 = 25 and 2 + 5 = 7. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> N = 65536, Dx :: 0..9, digital_root(N, Dx, 10), Vars = [Dx], solve(Vars), println(N=Dx), fail, nl. go => true. % % Excursions % go2 ?=> N = 65536, X :: 1..N, Dx :: 0..9, digital_root(X, Dx, 10), Vars = [X, Dx], solve(Vars), println([x=X,dx=Dx]), fail, nl. go2 => true. % % Another take. % go3 => M = 112, DRoots = new_list(M), DRoots :: 0..9, foreach(I in 1..M) digital_root(I, DRoots[I],10) end, Vars = DRoots, solve(Vars), println(droots=DRoots), nl, fail, nl. go3 => true. % % digital root % digital_root(Num, Res, Base) => Res #= 1 + ((Num-1) mod (Base-1)).