41
11
An Indian legend tells the story of the alleged inventor of the chess game, who impressed the emperor of India with his game so much that he would get rewarded with anything asked.
The man said he wanted to be paid in rice. He wanted a grain of rice for the first square of the chessboard, two for the second, four for the third, eight for the fourth, and so on, until the 64th square.
The emperor was amazed that the man asked for such a small reward, but as his mathematicians started counting, he ended up losing one of his provinces.
Task
Given the length of the side of a hypothetical chessboard (which is 8 on a default chessboard) and the multiplier between squares (which is 2 in the legend), calculate the number of grains of rice the emperor must pay to the man.
Notes
The side length will always be a positive integer. The multiplier could instead be any kind of rational number.
If your language of choice can't display very large numbers, it's okay as long as your program can correctly process smaller inputs.
Also if your language of choice rounds larger values (with exponential notations), it's okay if those values are approximately correct.
Testcases
Input (side length, multiplier) => Output
8, 2 => 18446744073709551615
3, 6 => 2015539
7, 1.5 => 850161998.2854
5, -3 => 211822152361
256, 1 => 65536
2, 2 => 15
2, -2 => -5
Please note that the explicit formula
result = (multiplier ^ (side ^ 2) - 1) / (multiplier - 1)
Performs wrong on multiplier = 1
, as
1 ^ (side ^ 2) - 1 = 0
1 - 1 = 0
0 / 0 != side ^ 2 (as it should be)
Scoring
This is code-golf. Shortest answer in bytes wins.
4You probably want a test case where the multiplier is 1 and another where it is 0 (assuming both are valid). Also "anything" is pretty broad, does the square root of negative one count? How about "potato"? ;) I'd recommend "any real number" or something. – FryAmTheEggman – 2016-05-03T14:58:27.260
4
If your language of choose can't display too large numbers, it's ok as long as your program can correctly process smaller inputs
Careful, that has caused problems in the past. http://meta.codegolf.stackexchange.com/a/8245/31716 – James – 2016-05-03T17:11:23.027Related: http://math.stackexchange.com/questions/203824/what-is-xy-how-to-understand-it/204461#204461
– Chloe – 2016-05-03T18:25:42.31724... it must have been a rich province, because even today, the yearly world production of rice is still less than 2^64 grains. – vsz – 2016-05-03T18:36:41.330
So you ask us to implement an explicit formula?
f(s,m)=(m^(s^2)-1)/(m-1)
? I bet that is a dupe. – flawr – 2016-05-03T19:39:25.640I thought the formula would make this trivial, but it not working for
m=1
means that both summing a list and a recursive/iterative expression seem to be competitive options in non-golfing languages. – xnor – 2016-05-03T19:57:46.8701@vsz Actually, the guy was killed. The amount added to the king giving away the entire kingdom to the man, so naturally the easier way out was taken. – cst1992 – 2016-05-04T08:13:33.347
1@cst1992 the version I read says the man gave up on his request and got a province as a gift. – user6245072 – 2016-05-04T09:09:39.433
1
result = ... / (multiplier - 1)
, not + – Pavel Mayorov – 2016-05-06T11:21:07.853This is
s^m^2-1
, right? – CalculatorFeline – 2017-06-18T16:43:46.413