Skip to content

Instantly share code, notes, and snippets.

@jonesor
Last active July 18, 2026 20:17
Show Gist options
  • Select an option

  • Save jonesor/3141b31cc5f3920b4830 to your computer and use it in GitHub Desktop.

Select an option

Save jonesor/3141b31cc5f3920b4830 to your computer and use it in GitHub Desktop.
# Rice on a chessboard: 1 grain on the first square, doubling each square.
# Vectorised: square i holds 2^(i-1) grains.
RiceOnASquare <- 2^(0:63)
sum(RiceOnASquare)
# NOTE: the true total is 2^64 - 1 = 18,446,744,073,709,551,615, which exceeds
# R's exact double-precision integer range (2^53). For the exact value use gmp:
# library(gmp); sum(as.bigz(2)^(0:63))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment