/* Cube sum problem in Picat. From Krzysztof R. Apt & Mark G. Wallace "Constraint Logic Programming using ECLiPSe", page 253 """ Problem: Given m check whether it can be written as a sum of at least two different cubes. If yes, produce the smallest solution in the number of cubes. """ 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 ?=> M = 1000000, cube_sum(M,X), println(m=M), println(x=X), % fail, nl. go => true. % % All solutions for M=1000 % go2 ?=> M = 10_000, cube_sum(M,X), println(M=X), fail, nl. go2 => true. % % Check first solution for 2..1000 % % The numbers(below 1000) with non unique solutions (below 1000) are the % following (on the form: M = Number of Solutions): % % 217 = 2, 224 = 2, 225 = 2, 540 = 2, 559 = 2, 560 = 2, 567 = 2, 568 = 2, % 728 = 2, 729 = 2, 736 = 2, 737 = 3, 756 = 3, 757 = 2, 764 = 2, 793 = 2, % 801 = 2, 820 = 2, 828 = 2, 854 = 2, 855 = 2, 862 = 2, 863 = 2, 881 = 2, % 882 = 2, 889 = 2, 890 = 2, 918 = 2, 919 = 2, 926 = 2, 927 = 2, 945 = 3, % 946 = 3, 953 = 3, 954 = 3, 980 = 2, 981 = 2 % % The first number with two solutions is 217 % The first number with three solution is 737 % go3 ?=> ManySolutions = [], foreach(M in 1..1000) if cube_sum(M,X) then println(M=X), Count=count_all(cube_sum(M,_)), if Count > 1 then ManySolutions := ManySolutions ++ [M=Count] end end end, println(manySolutions=ManySolutions). go3 => true. % % What are the result of 1^3+2^3+3^3..+N^3 % go4 => foreach(N in 1..20) L = [J**3 : J in 1..N], println(N=sum(L)) end, nl. cube_sum(M,X) => member(N,2..100), X = new_list(N), X :: 1..ceiling(M**(1/3)), M #= sum([X[I]**3 : I in 1..N]), all_different(X), increasing(X), solve($[degree,updown],X).