The rice and chess problem

MATL, 6 bytes

2^:q^s

Try it online!

2^   % Take implicit input, say N, and square it: N^2
:q   % Generate array [0 1 ... N^2-1]
^    % Take implicit input, M, and compute [M^0 M^1 ... M^(N^2-1)]
s    % Sum of the array. Implicit display

APL, 10 bytes

⎕⊥1+0×⍳⎕*2

is used to read user input twice. If we store the side length in s and the multiplier in m, we get the following code.

m⊥1+0×⍳s*2

And here's how APL parses this code:

Explanation of algorithm


Jelly, 4 bytes

²b1ḅ

This uses the approach from @APLDude's clever APL answer.

Try it online! or verify all test cases.

How it works

²b1ḅ  Main link. Arguments: x (side length), y (multiplier)

²     Square; yield x².
 b1   Convert to base 1 (unary), yielding a list of x² ones.
   ḅ  Convert from base y to real number.
      This yields y^(x²-1) + ... + y + 1.

Tags:

Math

Code Golf