/* Divisible by unit digits in Picat. Martin Gardner problem (picture) http://3.bp.blogspot.com/-p7UH0kTXF8A/TWwfpmdSsmI/AAAAAAAACkw/s0kN9x4pkOg/s1600/mm.gif """ The number 64 har the property that is divisivle by its units digits. How many whole numbers between 10 and 50 has this property? """ (Via OscaR.) 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 ?=> A=[I : I in 11..50, X=I mod 10,X>0, Y = I div 10, Y>0, I mod X == 0, I mod Y ==0], println(A), nl. go => true. % % CP approach % go2 ?=> N :: 11..50, N mod 10 #> 0, N mod (N mod 10) #= 0, N mod (N div 10) #= 0, solve([N]), println(N), fail, nl. go2 => true. % % CP approach II % go3 ?=> N :: 11..50, A = new_list(2), A :: 0..9, A[2] #> 0, to_num(A,10,N), N mod A[1] #= 0, N mod A[2] #= 0, solve([N]), println(N), fail, nl. go3 => true. % % Let's run further % go4 => All = [N : N in 11..1_000_000, divisible_by_digits(N)], println(All), println(len=All.len), nl. % % There's a lot of mod 11 number. Let's compare how many numbers that's % are divisible by unit digits with and without the mod 11 numbers. % % 100 = [all = 14,no_mod_11 = 5] % 1000 = [all = 70,no_mod_11 = 58] % 10000 = [all = 330,no_mod_11 = 288] % 100000 = [all = 1636,no_mod_11 = 1486] % 1000000 = [all = 9030,no_mod_11 = 8151] % 10000000 = [all = 52981,no_mod_11 = 48192] % 100000000 = [all = 326490,no_mod_11 = 296371] % go5 => foreach(P in 2..8) All = [N : N in 11..10**(P)-1, divisible_by_digits(N)], No_11 = [N : N in All, N mod 11 != 0], % skip mod 11 numbers println((10**P)=[all=All.len,no_mod_11=No_11.len]) end, nl. % % No CP. % divisible_by_digits(N) => All = true, foreach(I in N.to_string, All == true) if I == '0' ; N mod I.to_int != 0 then All := false end end, All == true. % % Using CP % divisible_by_digits_cp(N) => Len = ceiling(log10(N+1)), X = new_list(Len), X :: 0..9, to_num(X,10,N), foreach(I in 1..Len) N mod X[I] #= 0 end, solve(X). % % 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]).