/* Harshad numbers in Picat. http://en.wikipedia.org/wiki/Harshad_number """ A Harshad number, or Niven number in a given number base, is an integer that is divisible by the sum of its digits when written in that base. Harshad numbers were defined by D. R. Kaprekar, a mathematician from India. The word "Harshad" comes from the Sanskrit harį¹£a (joy) + da (give), meaning joy-giver. The Niven numbers take their name from Ivan M. Niven from a paper delivered at a conference on number theory in 1977. All integers between zero and n are Harshad numbers in base n. """ Also see: http://oeis.org/A005349 1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,42,45,48,50,54,60,63,70,72,80,81,84,90,100,102,108,110,111,112,114,117,120,126,132,133,135,140,144,150,152,153,156,162,171,180,190,192,195,198,200,201,204 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 => Limit = 10000, Base = 10, X :: 1..Limit, IntLen = int_len(Limit,Base), List = new_list(IntLen+1), List :: 0..Base-1, to_num(List, Base, X), X mod sum(List) #= 0, % Vars = List ++ [X], All=solve_all(X), writeln(all=All), % writeln(list=List), nl. % check if N is a Harshad number harshad(N, Base) => IntLen = int_len(N,Base), List = new_list(IntLen+1), List :: 0..Base-1, X #= N, to_num(List, Base, X), X mod sum(List) #= 0, Vars = List ++ [X], solve(Vars). % % converts a number Num to/from a list of integer List given a base Base % to_num(List, Base, Num) => Len = length(List), Num #= sum([List[I]*Base**(Len-I) : I in 1..Len]). int_len(V,Base) = Len => Len1=1, while (V > Base-1) Len1 := Len1 + 1, V := V div Base end, Len = Len1.