Skip to content

Commit 43e3e21

Browse files
📦 NEW: Added solution for ProjectEuler-017
1 parent 0f9f1ba commit 43e3e21

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

Project-Euler/Problem017.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Number letter counts
3+
*
4+
* P.S.(Project Euler - 017):
5+
* If the numbers 1 to 5 are written out in words: one, two, three, four, five,
6+
* then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
7+
*
8+
* If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words,
9+
* how many letters would be used?
10+
*/
11+
12+
const ones = {
13+
1: 'one',
14+
2: 'two',
15+
3: 'three',
16+
4: 'four',
17+
5: 'five',
18+
6: 'six',
19+
7: 'seven',
20+
8: 'eight',
21+
9: 'nine',
22+
10: 'ten',
23+
11: 'eleven',
24+
12: 'twelve',
25+
13: 'thirteen',
26+
14: 'fourteen',
27+
15: 'fifteen',
28+
16: 'sixteen',
29+
17: 'seventeen',
30+
18: 'eighteen',
31+
19: 'nineteen'
32+
}
33+
34+
const tens = {
35+
2: 'twenty',
36+
3: 'thirty',
37+
4: 'forty',
38+
5: 'fifty',
39+
6: 'sixty',
40+
7: 'seventy',
41+
8: 'eighty',
42+
9: 'ninety'
43+
}
44+
45+
/**
46+
* Converts Number into Word
47+
* @param {Number} num
48+
* @returns {String} returns the converted word format of the number
49+
*/
50+
function convertNumberToLetter (num) {
51+
const words = []
52+
53+
if (num >= 1000) {
54+
words.push(ones[Math.floor(num / 1000)])
55+
words.push('thousand')
56+
num %= 1000
57+
}
58+
59+
if (num >= 100) {
60+
words.push(ones[Math.floor(num / 100)])
61+
words.push('hundred')
62+
if (num % 100 !== 0) {
63+
words.push('and')
64+
}
65+
num %= 100
66+
}
67+
68+
if (num >= 20) {
69+
words.push(tens[Math.floor(num / 10)])
70+
num %= 10
71+
}
72+
73+
if (ones[num]) {
74+
words.push(ones[num])
75+
}
76+
77+
return words.join('')
78+
}
79+
80+
/**
81+
* Count total letters of converted words between two numbers
82+
* @param {Number} lowNum
83+
* @param {Number} highNum
84+
* @returns {Number} returns the count of total letters of converted words
85+
*/
86+
export const countLetters = (lowNum, highNum) => {
87+
if (lowNum > highNum) {
88+
throw new Error('Minimum number can\'t be greater than Maximum number')
89+
}
90+
if ([lowNum, highNum].some(num => num < 1)) {
91+
throw new Error('Insufficient information provided for the input')
92+
}
93+
const words = []
94+
for (let i = lowNum; i <= highNum; i++) {
95+
words.push(convertNumberToLetter(i))
96+
}
97+
return words.join('').length
98+
}

Project-Euler/test/Problem017.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { countLetters } from '../Problem017.js'
2+
3+
describe('Number letter counts', () => {
4+
it('should throw error when min number is greater than max number', () => {
5+
expect(() => countLetters(3, 1)).toThrowError('Minimum number can\'t be greater than Maximum number')
6+
})
7+
it('should throw error when min number is less than 1', () => {
8+
expect(() => countLetters(-5, 10)).toThrowError('Insufficient information provided for the input')
9+
})
10+
it('should throw error when max number is less than 1', () => {
11+
expect(() => countLetters(0, 0)).toThrowError('Insufficient information provided for the input')
12+
})
13+
test('if the numbers are greater than 1', () => {
14+
expect(countLetters(1, 5)).toBe(19)
15+
})
16+
// Project Euler Condition Check
17+
test('if the numbers are 1 and 1000 respectively', () => {
18+
expect(countLetters(1, 1000)).toBe(21124)
19+
})
20+
})

0 commit comments

Comments
 (0)