/*
Euler #29 in Picat.
"""
Consider all integer combinations of a^b for 2 <= a <= 5 and 2 <= b <= 5:
2^2=4, 2^3=8, 2^4=16, 2^5=32
3^2=9, 3^3=27, 3^4=81, 3^5=243
4^2=16, 4^3=64, 4^4=256, 4^5=1024
5^2=25, 5^3=125, 5^4=625, 5^5=3125
If they are then placed in numerical order, with any repeats removed, we get the
following sequence of 15 distinct terms:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
How many distinct terms are in the sequence generated by a^b for
2 <= a <= 100 and 2 <= b <= 100?
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
main => go.
go => time(euler29). % , time(euler29b).
% 0.180s
euler29 =>
Min = 2,
Max = 100,
Hash = new_map(),
foreach (A in Min..Max, B in Min..Max)
Hash.put(A**B,1)
end,
println(Hash.keys().length).
% 0.188s
euler29b =>
Min = 2,
Max = 100,
println(new_map([(A**B=1) : A in Min..Max, B in Min..Max]).keys().length).
% 1.267s
euler29c =>
Range = 2..100,
println([A**B : A in Range, B in Range].remove_dups().length).
% 1.22s
euler29d =>
Min = 2,
Max = 100,
Found = [],
foreach(A in Min..Max, B in Min..Max)
AB = A**B,
if not(membchk(AB, Found)) then
Found := Found ++ [AB]
end
end,
println(Found.length).
% 0.188s
euler29e =>
Min = 2,
Max = 100,
Found = new_map(),
foreach(A in Min..Max, B in Min..Max)
AB = A**B,
if not(Found.has_key(AB)) then
Found.put(AB,1)
end
end,
println(Found.keys().length).