Skip to content

Commit 86a397a

Browse files
joshiraezSleeplessByte
authored andcommitted
Refactored grains to use bigint in the example and tests. Also left a note to use bigint in the problem description
1 parent 9fdd825 commit 86a397a

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

exercises/practice/grains/.docs/instructions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Write code that shows:
1616
- how many grains were on a given square, and
1717
- the total number of grains on the chessboard
1818

19+
You will need to use the [bigint](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html#bigint) type in this problem, as the usual `number` type loses precision when on big numbers, being a floating point based storage.
20+
1921
## For bonus points
2022

2123
Did you get the tests passing and the code clean? If you want to, these

exercises/practice/grains/.meta/proof.ci.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
export function square(square: number): number {
1+
export function square(square: number): bigint {
22
if (square <= 0 || square >= 65) {
33
throw new Error()
44
}
55

6-
return Math.pow(2, square - 1)
6+
return BigInt(square) ** 2n
77
}
88

9-
export function total(): number {
10-
let total = 0
9+
export function total(): bigint {
10+
let total = 0n
1111

1212
for (let i = 1; i <= 64; i++) {
13-
total += square(i)
13+
total += this.square(i)
1414
}
1515

1616
return total

exercises/practice/grains/grains.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@ import { square, total } from './grains'
22

33
describe('returns the number of grains on the square', () => {
44
it('1', () => {
5-
const expected = 1
5+
const expected = 1n
66
expect(square(1)).toEqual(expected)
77
})
88

99
xit('2', () => {
10-
const expected = 2
10+
const expected = 2n
1111
expect(square(2)).toEqual(expected)
1212
})
1313

1414
xit('3', () => {
15-
const expected = 4
15+
const expected = 4n
1616
expect(square(3)).toEqual(expected)
1717
})
1818

1919
xit('4', () => {
20-
const expected = 8
20+
const expected = 8n
2121
expect(square(4)).toEqual(expected)
2222
})
2323

2424
xit('16', () => {
25-
const expected = 32768
25+
const expected = 32768n
2626
expect(square(16)).toEqual(expected)
2727
})
2828

2929
xit('32', () => {
30-
const expected = 2147483648
30+
const expected = 2147483648n
3131
expect(square(32)).toEqual(expected)
3232
})
3333

3434
xit('64', () => {
35-
const expected = 9223372036854775808
35+
const expected = 9223372036854775808n
3636
expect(square(64)).toEqual(expected)
3737
})
3838

@@ -51,7 +51,7 @@ describe('returns the number of grains on the square', () => {
5151

5252
describe('returns the total number of grains on the board', () => {
5353
xit('total', () => {
54-
const expected = 18446744073709551615
54+
const expected = 18446744073709551615n
5555
expect(total()).toEqual(expected)
5656
})
5757
})

exercises/practice/grains/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
/* Basic Options */
4-
"target": "es2017" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
4+
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
55
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
66
"lib": [
77
"esnext",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@
6363
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
6464
},
6565
"compileOnSave": true,
66-
"exclude": ["node_modules"]
66+
"exclude": ["node_modules", "exercises"]
6767
}

0 commit comments

Comments
 (0)