This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eac4c15
commit 347e96c
Showing
7 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Instructions | ||
|
||
Calculate the number of grains of wheat on a chessboard given that the number on each square doubles. | ||
|
||
There once was a wise servant who saved the life of a prince. | ||
The king promised to pay whatever the servant could dream up. | ||
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. | ||
One grain on the first square of a chess board, with the number of grains doubling on each successive square. | ||
|
||
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on). | ||
|
||
Write code that shows: | ||
|
||
- how many grains were on a given square, and | ||
- the total number of grains on the chessboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"ErikSchierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/grains.cljs" | ||
], | ||
"test": [ | ||
"test/grains_test.cljs" | ||
], | ||
"example": [ | ||
".meta/src/example.cljs" | ||
] | ||
}, | ||
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.", | ||
"source": "The CodeRanch Cattle Drive, Assignment 6", | ||
"source_url": "https://coderanch.com/wiki/718824/Grains" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(ns grains) | ||
|
||
(defn- pow [x n] | ||
(loop [x x n n r 1] | ||
(cond | ||
(= n 0) r | ||
(even? n) (recur (* x x) (/ n 2) r) | ||
:else (recur x (dec n) (* r x))))) | ||
|
||
(defn square [number] | ||
(pow 2 (dec number))) | ||
|
||
(def ^:private square-numbers (rest (range 65))) | ||
|
||
(defn total [] | ||
(apply + (map square square-numbers))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{:deps | ||
{org.clojure/clojure {:mvn/version "1.10.1"} | ||
org.clojure/clojurescript {:mvn/version "1.10.773"}} | ||
|
||
:aliases | ||
{:test | ||
{:extra-paths ["test"] | ||
:extra-deps | ||
{olical/cljs-test-runner {:mvn/version "3.8.0"}} | ||
:main-opts ["-m" "cljs-test-runner.main"]}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(ns grains) | ||
|
||
(defn square [] ;; <- arglist goes here | ||
;; your code goes here | ||
) | ||
|
||
(defn total [] ;; <- arglist goes here | ||
;; your code goes here | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(ns grains-test | ||
(:require [clojure.test :refer [deftest is]] | ||
grains)) | ||
|
||
(deftest square-1 | ||
(is (= 1 (grains/square 1)))) | ||
|
||
(deftest square-2 | ||
(is (= 2 (grains/square 2)))) | ||
|
||
(deftest square-3 | ||
(is (= 4 (grains/square 3)))) | ||
|
||
(deftest square-4 | ||
(is (= 8 (grains/square 4)))) | ||
|
||
(deftest square-16 | ||
(is (= 32768 (grains/square 16)))) | ||
|
||
(deftest square-32 | ||
(is (= 2147483648 (grains/square 32)))) | ||
|
||
(deftest square-64 | ||
(is (= 9223372036854775808 (grains/square 64)))) | ||
|
||
(deftest total-grains | ||
(is (= 18446744073709551615 (grains/total)))) |