Skip to content

Latest commit

 

History

History
46 lines (40 loc) · 1.62 KB

index.org

File metadata and controls

46 lines (40 loc) · 1.62 KB

Exercism.io → Racket → Grains

grains.rkt

Use the ~#lang~ shorthand to declare the grains module.
#lang racket

The functions grains.rkt needs to export are square and total.

(provide square total)

square

The definition of square is simple: $\displaystyle \text{square}\ n = 2n - 1$

(define (square n) (expt 2 (sub1 n)))

total

The total is the sum of the number of grains of wheat on every square, i.e. $\displaystyle ∑n=164 2n - 1$.

\begin{align*} ∑n=1k 2n - 1 &= 2^k &- 1
n=164 2n - 1 &= 264 &- 1 \ \text{total} &= 265 - 1 &- 1 \ &= \text{square}\ 65 &- 1 \end{align*}

(define (total) (sub1 (square 65)))