Sha256: 0e37e52fab825b5b6b10fba9685b5154c4bd4ecd65564ecfff1ea330c69a962f

Contents?: true

Size: 768 Bytes

Versions: 396

Compression:

Stored size: 768 Bytes

Contents

import BigInt from './big-integer';

/**
 * Computes the number of grains on the squares of a
 *
 * chess board, starting with one grain on the first
 * square, and doubling with each successive square.
 */
export default class Grains {

  /**
   * Gets the number of grains on the nth square
   *
   * @param {Number} num Number to compute its square
   *
   * @return {String} Square of num
   */
  square(num) {
    return BigInt(2).pow(num - 1).toString();
  }

  /**
   * Gets the total number of grains on all squares
   *
   * @return {String} Sum of all squares
   */
  total() {
    let total = BigInt(0);

    for (let squareNum = 1; squareNum <= 64; squareNum++) {
      total = total.add(this.square(squareNum));
    }

    return total.toString();
  }
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/grains/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/grains/example.js