/* From Jesper Larsson https://www.facebook.com/avadeaux/posts/pfbid029558BqnYwKP5oagSwYk5jnMBTWbg699aPj6fwCC2rD9qBE4td8aFraLjXYfw2NXql """ I want a simple expression that given a number of bits i from 1 to 32 tells me the size of the smallest integer type that has at least that many bits. That is, for i = 1…8 it’s 1, for i = 9…16 it’s 2, and for i = 17…32 it’s 4. It should work in Java, and I don’t want any “?:”, but any logical or arithmetic operations are fine. The smallest one I’ve come up with is the one in the picture. Can you beat it? """ [In a comment: The value for i=1 should be 1 The value for i=2 should be 1 The value for i=3 should be 1 The value for i=4 should be 1 The value for i=5 should be 1 The value for i=6 should be 1 The value for i=7 should be 1 The value for i=8 should be 1 The value for i=9 should be 2 The value for i=10 should be 2 The value for i=11 should be 2 The value for i=12 should be 2 The value for i=13 should be 2 The value for i=14 should be 2 The value for i=15 should be 2 The value for i=16 should be 2 The value for i=17 should be 4 The value for i=18 should be 4 The value for i=19 should be 4 The value for i=20 should be 4 The value for i=21 should be 4 The value for i=22 should be 4 The value for i=23 should be 4 The value for i=24 should be 4 The value for i=25 should be 4 The value for i=26 should be 4 The value for i=27 should be 4 The value for i=28 should be 4 The value for i=29 should be 4 The value for i=30 should be 4 The value for i=31 should be 4 The value for i=32 should be 4 ] """ Blog post: Computing the integer size of a sample https://klipspringer.avadeaux.net/computing-the-integer-size-of-a-sample * Solutions (using [+,-,*,div] and init_size=1000 # This is not correct (caused by a typo in my f_jesper/1) # * This took 51 min (with reset_timeout set to 600 seconds) # (27 + X) div 18 + X div 27 This is the most interesting one: - 1h07min41s X div X + 3 div (32 div X) Note X div X -> 1 -> 1 + 3 div (32 div X) Some other solutions: - 21 div (X - (X - X * X)) + 4) div ((16 + X) div X) - (X div (10 - 25) + 24 div X - X * (27 - 14) + 19 * X - X) div X div ((17 - X - 1) div X + 2) - (X + 4) div X * 8 div ((X + 32) div X) - (X + 15) div 16 * (X div X + (X + X) div (10 * 2 div X div (X * 17) + (X - 2) + 11)) - (4 + 20 div X div X) div ((16 + X) div X) - ((2 * 29 div X + X) div X + 7) div ((18 + 15 + X) div X) This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ data(jesper_bits,Data,Vars,Unknown,Ops,Constants,MaxSize,Params) :- garbage_collect(500_000_000), % Data = [ [[I],f_jesper(I) ] : I in 1..32], Data = [[[1], 1], [[2], 1], [[3], 1], [[4], 1], [[5], 1], [[6], 1], [[7], 1], [[8], 1], [[9], 2], [[10], 2], [[11], 2], [[12], 2], [[13], 2], [[14], 2], [[15], 2], [[16], 2], [[17], 4], [[18], 4], [[19], 4], [[20], 4], [[21], 4], [[22], 4], [[23], 4], [[24], 4], [[25], 4], [[26], 4], [[27], 4], [[28], 4], [[29], 4], [[30], 4], [[31], 4], [[32], 4]], % Ops = [+,-,*,div,>>], Ops = [+,-,*,div], % Just division % Ops = [+,-,*,\/,/\], % Constants = 1..64, Constants = 1..32, % Smaller domain Vars = ['X'], Unknown = [3], % MaxSize = 15, MaxSize = 11, Params = new_map([init_size=1000, % num_gens=120 reset_timeout = 600 % reset problem after reset_timeout seconds ]). % Jesper's solution f_jesper(I) = 1 + I//9 + I//17 * 2 - I//18 - I//27.