40
\$\begingroup\$

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.

\$\endgroup\$
10
  • 4
    \$\begingroup\$ You 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. \$\endgroup\$ Commented May 3, 2016 at 14:58
  • 4
    \$\begingroup\$ 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. meta.codegolf.stackexchange.com/a/8245/31716 \$\endgroup\$
    – DJMcMayhem
    Commented May 3, 2016 at 17:11
  • 24
    \$\begingroup\$ ... it must have been a rich province, because even today, the yearly world production of rice is still less than 2^64 grains. \$\endgroup\$
    – vsz
    Commented May 3, 2016 at 18:36
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – cst1992
    Commented May 4, 2016 at 8:13
  • 1
    \$\begingroup\$ @cst1992 the version I read says the man gave up on his request and got a province as a gift. \$\endgroup\$ Commented May 4, 2016 at 9:09

40 Answers 40

1
2
1
\$\begingroup\$

Actually, 9 bytes

╗²r`╜ⁿ`MΣ

Takes input as side length first, then multiplier.

Try it online!

Explanation:

╗²r`╜ⁿ`MΣ
╗          save m to reg0
 ²r        range(s**2) ([0, s**2-1])
   `  `M   map (for n in list):
    ╜ⁿ       m**n (m is pushed from reg0)
        Σ  sum
\$\endgroup\$
1
\$\begingroup\$

Pyke, 5 bytes

XUm^s

Try it here!

X     - input()**2
 U   - range(0,^)
  m^  - map(^,^)
    s - sum(^)
\$\endgroup\$
1
\$\begingroup\$

J, 17 bytes

A non-tacit solution, by defining a dyad:

f=:4 :'+/y^i.x^2'

e.g.

8x f 2

18446744073709551615
\$\endgroup\$
1
\$\begingroup\$

MathGolf, 4 bytes

²r#Σ

Try it online!

Explanation

Basically a port of Luis Mendo's MATL answer, but uses some implicit operating magic of MathGolf.

²      Square the first input
 r     Create range [0, 1, ..., N^2-1]
  #    Map (2nd input)^i for i in the array
   Σ   Sum the array
\$\endgroup\$
1
\$\begingroup\$

Thunno 2 S, 3 bytes

²L*

Try it online!

Port of Luis Mendo's MATL answer.

Explanation

²L*  # Implicit input
²    # Square the first input
 L   # Push [0..that)
  *  # Take the second input
     # to the power of each
     # Sum the resulting list
     # Implicit output
\$\endgroup\$
0
\$\begingroup\$

C#, 48 bytes

x=>y=>x==1?y*y:(System.Math.Pow(x,y*y)-1)/(x-1);

Where x is the multiplier, y is the side length. It uses the explicit formula for the sum of the geometric series, which is a1*(q^n-1)/(q-1), if q!=1, n*a1, if q==1.

\$\endgroup\$
0
\$\begingroup\$

k, 16 11 bytes

Bytes shaved using the technique from a clever answer.

{y/(x*x)#1}

Try it online! (There seems to be some sort of rounding happening for large numbers.)

Explanation:

{         } /function, x is board length, y is multiplier
   (x*x)#1  /make a list with as many 1s as there are board squares
 y/         /read as base y number
\$\endgroup\$
0
\$\begingroup\$

Powershell, 48 bytes

param($l,$m,$r)$c=1;1..$l*$l|%{$r+=$c;$c*=$m};$r

Test script:

$f = {
    param($l,$m,$r)$c=1;1..$l*$l|%{$r+=$c;$c*=$m};$r
}

@(
    ,(8, 2    ,18446744073709551615)
    ,(3, 6    ,2015539)
    ,(7, 1.5  ,850161998.2854)
    ,(5, -3   ,211822152361)
    ,(256, 1  ,65536)
    ,(2, 2    ,15)
    ,(2, -2   ,-5)
) | %{
    $x,$n,$e = $_
    $r = &$f $x $n
    "$($e-eq$r): $r $e"
}

Output:

False: 1.84467440737096E+19 18446744073709551615
True: 2015539 2015539
False: 850161998.285399 850161998.2854
True: 211822152361 211822152361
True: 65536 65536
True: 15 15
True: -5 -5

Differences from rounding are permitted by rules.

$r is a local parameter. If a parameter omitted in a caller then $r will have the value $null.

\$\endgroup\$
0
\$\begingroup\$

Japt -x, 5 bytes

²o!pV

Try it

\$\endgroup\$
1
  • \$\begingroup\$ Could you add an explanation of your solution for those not familiar with Japt? \$\endgroup\$
    – mkst
    Commented Jun 17, 2020 at 9:55
0
\$\begingroup\$

F#, 51 bytes

let c s m=Seq.init(s*s)(fun x->m**float x)|>Seq.sum

Fairly straight-forward. Works as well when m is 1.

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.