/ruby/grains/grains.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Grains
  @@num_squares = 64

  def square(n)
    # Return number of grains on square number n
    2**(n-1)
  end

  def total
    # Return total number of grains on all squares
    # sum(2**1 .. 2**(n-1)) = (2**n) - 1
    (2**@@num_squares) - 1
  end
end

@sohaibbhatti thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment