feat(euler): 42 - coded triangle numbers
This commit is contained in:
parent
630aea3baa
commit
6d6088bb0f
4 changed files with 71 additions and 2 deletions
|
@ -4,5 +4,33 @@ module.exports = {
|
||||||
resources: path.join(__dirname, 'resources'),
|
resources: path.join(__dirname, 'resources'),
|
||||||
src: path.join(__dirname, 'src'),
|
src: path.join(__dirname, 'src'),
|
||||||
thoughts: path.join(__dirname, 'thoughts'),
|
thoughts: path.join(__dirname, 'thoughts'),
|
||||||
root: __dirname
|
root: __dirname,
|
||||||
|
alphabet: [
|
||||||
|
'a',
|
||||||
|
'b',
|
||||||
|
'c',
|
||||||
|
'd',
|
||||||
|
'e',
|
||||||
|
'f',
|
||||||
|
'g',
|
||||||
|
'h',
|
||||||
|
'i',
|
||||||
|
'j',
|
||||||
|
'k',
|
||||||
|
'l',
|
||||||
|
'm',
|
||||||
|
'n',
|
||||||
|
'o',
|
||||||
|
'p',
|
||||||
|
'q',
|
||||||
|
'r',
|
||||||
|
's',
|
||||||
|
't',
|
||||||
|
'u',
|
||||||
|
'v',
|
||||||
|
'w',
|
||||||
|
'x',
|
||||||
|
'y',
|
||||||
|
'z'
|
||||||
|
]
|
||||||
};
|
};
|
||||||
|
|
|
@ -53,7 +53,7 @@ The source code can be found in the [src](src) directory. My thoughts about some
|
||||||
- [ ] 39 - Integer right triangles
|
- [ ] 39 - Integer right triangles
|
||||||
- [ ] 40 - Champernowne's constant
|
- [ ] 40 - Champernowne's constant
|
||||||
- [ ] 41 - Pandigital prime
|
- [ ] 41 - Pandigital prime
|
||||||
- [ ] 42 - Coded triangle numbers
|
- [x] [42 - Coded triangle numbers](src/42%20-%20Coded%20triangle%20numbers.ts)
|
||||||
- [ ] 43 - Sub-string divisibility
|
- [ ] 43 - Sub-string divisibility
|
||||||
- [ ] 44 - Pentagon numbers
|
- [ ] 44 - Pentagon numbers
|
||||||
- [ ] 45 - Triangular, pentagonal, and hexagonal
|
- [ ] 45 - Triangular, pentagonal, and hexagonal
|
||||||
|
|
1
challenges/euler/resources/p042_words.txt
Normal file
1
challenges/euler/resources/p042_words.txt
Normal file
File diff suppressed because one or more lines are too long
40
challenges/euler/src/42 - Coded triangle numbers.ts
Normal file
40
challenges/euler/src/42 - Coded triangle numbers.ts
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
// The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
|
||||||
|
// 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
|
||||||
|
// By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
|
||||||
|
// Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
|
||||||
|
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import { resources, alphabet } from '../constants.js';
|
||||||
|
export = {};
|
||||||
|
|
||||||
|
const words = fs
|
||||||
|
.readFileSync(path.join(resources, 'p042_words.txt'))
|
||||||
|
.toString()
|
||||||
|
.split(',')
|
||||||
|
.map(word => word.replace(/"/g, '').toLowerCase());
|
||||||
|
|
||||||
|
const isTriangleNumber = (t: number) => {
|
||||||
|
// One solution of the inverse triangle number formula
|
||||||
|
const solution = (Math.sqrt(8 * t + 1) - 1) / 2;
|
||||||
|
|
||||||
|
// This number must be an integer and greater than zero for it to be a triangle number
|
||||||
|
return Number.isInteger(solution) && solution > 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
let triangleWordCount = 0;
|
||||||
|
|
||||||
|
for (const word of words) {
|
||||||
|
const letters = word.split('');
|
||||||
|
let sum = 0;
|
||||||
|
|
||||||
|
for (const letter of letters) {
|
||||||
|
const value = alphabet.indexOf(letter) + 1;
|
||||||
|
sum += value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTriangleNumber(sum)) triangleWordCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output
|
||||||
|
console.log(`Out of nearly 2000 common English words, ${triangleWordCount} are triangle words!`);
|
Loading…
Reference in a new issue