Skip to content

Commit 9135353

Browse files
authored
Add sum-of-multiples (#354)
1 parent b4d2836 commit 9135353

File tree

8 files changed

+185
-0
lines changed

8 files changed

+185
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,14 @@
535535
"prerequisites": [],
536536
"difficulty": 2
537537
},
538+
{
539+
"slug": "sum-of-multiples",
540+
"name": "Sum of Multiples",
541+
"uuid": "2e6dcbe2-bef0-4f37-b048-7701fe116c4c",
542+
"practices": [],
543+
"prerequisites": [],
544+
"difficulty": 2
545+
},
538546
{
539547
"slug": "triangle",
540548
"name": "Triangle",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Your task is to write the code that calculates the energy points that get awarded to players when they complete a level.
4+
5+
The points awarded depend on two things:
6+
7+
- The level (a number) that the player completed.
8+
- The base value of each magical item collected by the player during that level.
9+
10+
The energy points are awarded according to the following rules:
11+
12+
1. For each magical item, take the base value and find all the multiples of that value that are less than the level number.
13+
2. Combine the sets of numbers.
14+
3. Remove any duplicates.
15+
4. Calculate the sum of all the numbers that are left.
16+
17+
Let's look at an example:
18+
19+
**The player completed level 20 and found two magical items with base values of 3 and 5.**
20+
21+
To calculate the energy points earned by the player, we need to find all the unique multiples of these base values that are less than level 20.
22+
23+
- Multiples of 3 less than 20: `{3, 6, 9, 12, 15, 18}`
24+
- Multiples of 5 less than 20: `{5, 10, 15}`
25+
- Combine the sets and remove duplicates: `{3, 5, 6, 9, 10, 12, 15, 18}`
26+
- Sum the unique multiples: `3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 = 78`
27+
- Therefore, the player earns **78** energy points for completing level 20 and finding the two magical items with base values of 3 and 5.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
You work for a company that makes an online, fantasy-survival game.
4+
5+
When a player finishes a level, they are awarded energy points.
6+
The amount of energy awarded depends on which magical items the player found while exploring that level.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"sum-of-multiples.coffee"
8+
],
9+
"test": [
10+
"sum-of-multiples.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Given a number, find the sum of all the multiples of particular numbers up to but not including that number.",
17+
"source": "A variation on Problem 1 at Project Euler",
18+
"source_url": "https://projecteuler.net/problem=1"
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class SumOfMultiples
2+
@sum: (factors, limit) ->
3+
multiples = []
4+
for i in [1..limit - 1]
5+
for factor in factors
6+
if i % factor is 0
7+
multiples.push i
8+
break
9+
multiples.reduce (a, b) ->
10+
a + b
11+
, 0
12+
13+
module.exports = SumOfMultiples
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[54aaab5a-ce86-4edc-8b40-d3ab2400a279]
13+
description = "no multiples within limit"
14+
15+
[361e4e50-c89b-4f60-95ef-5bc5c595490a]
16+
description = "one factor has multiples within limit"
17+
18+
[e644e070-040e-4ae0-9910-93c69fc3f7ce]
19+
description = "more than one multiple within limit"
20+
21+
[607d6eb9-535c-41ce-91b5-3a61da3fa57f]
22+
description = "more than one factor with multiples within limit"
23+
24+
[f47e8209-c0c5-4786-b07b-dc273bf86b9b]
25+
description = "each multiple is only counted once"
26+
27+
[28c4b267-c980-4054-93e9-07723db615ac]
28+
description = "a much larger limit"
29+
30+
[09c4494d-ff2d-4e0f-8421-f5532821ee12]
31+
description = "three factors"
32+
33+
[2d0d5faa-f177-4ad6-bde9-ebb865083751]
34+
description = "factors not relatively prime"
35+
36+
[ece8f2e8-96aa-4166-bbb7-6ce71261e354]
37+
description = "some pairs of factors relatively prime and some not"
38+
39+
[624fdade-6ffb-400e-8472-456a38c171c0]
40+
description = "one factor is a multiple of another"
41+
42+
[949ee7eb-db51-479c-b5cb-4a22b40ac057]
43+
description = "much larger factors"
44+
45+
[41093673-acbd-482c-ab80-d00a0cbedecd]
46+
description = "all numbers are multiples of 1"
47+
48+
[1730453b-baaa-438e-a9c2-d754497b2a76]
49+
description = "no factors means an empty sum"
50+
51+
[214a01e9-f4bf-45bb-80f1-1dce9fbb0310]
52+
description = "the only multiple of 0 is 0"
53+
54+
[c423ae21-a0cb-4ec7-aeb1-32971af5b510]
55+
description = "the factor 0 does not affect the sum of multiples of other factors"
56+
57+
[17053ba9-112f-4ac0-aadb-0519dd836342]
58+
description = "solutions using include-exclude must extend to cardinality greater than 3"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class SumOfMultiples
2+
@sum: (factors, limit) ->
3+
4+
module.exports = SumOfMultiples
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
SumOfMultiples = require './sum-of-multiples'
2+
3+
describe 'Sum of Multiples', ->
4+
it 'no multiples within limit', ->
5+
expect(SumOfMultiples.sum [3, 5], 1).toEqual 0
6+
7+
xit 'one factor has multiples within limit', ->
8+
expect(SumOfMultiples.sum [3, 5], 4).toEqual 3
9+
10+
xit 'more than one multiple within limit', ->
11+
expect(SumOfMultiples.sum [3], 7).toEqual 9
12+
13+
xit 'more than one factor with multiples within limit', ->
14+
expect(SumOfMultiples.sum [3, 5], 10).toEqual 23
15+
16+
xit 'each multiple is only counted once', ->
17+
expect(SumOfMultiples.sum [3, 5], 100).toEqual 2318
18+
19+
xit 'a much larger limit', ->
20+
expect(SumOfMultiples.sum [3, 5], 1000).toEqual 233168
21+
22+
xit 'three factors', ->
23+
expect(SumOfMultiples.sum [7, 13, 17], 20).toEqual 51
24+
25+
xit 'factors not relatively prime', ->
26+
expect(SumOfMultiples.sum [4, 6], 15).toEqual 30
27+
28+
xit 'some pairs of factors relatively prime and some not', ->
29+
expect(SumOfMultiples.sum [5, 6, 8], 150).toEqual 4419
30+
31+
xit 'one factor is a multiple of another', ->
32+
expect(SumOfMultiples.sum [5, 25], 51).toEqual 275
33+
34+
xit 'much larger factors', ->
35+
expect(SumOfMultiples.sum [43, 47], 10000).toEqual 2203160
36+
37+
xit 'all numbers are multiples of 1', ->
38+
expect(SumOfMultiples.sum [1], 100).toEqual 4950
39+
40+
xit 'no factors means an empty sum', ->
41+
expect(SumOfMultiples.sum [], 10000).toEqual 0
42+
43+
xit 'the only multiple of 0 is 0', ->
44+
expect(SumOfMultiples.sum [0], 1).toEqual 0
45+
46+
xit 'the factor 0 does not affect the sum of multiples of other factors', ->
47+
expect(SumOfMultiples.sum [3, 0], 4).toEqual 3
48+
49+
xit 'solutions using include-exclude must extend to cardinality greater than 3', ->
50+
expect(SumOfMultiples.sum [2, 3, 5, 7, 11], 10000).toEqual 39614537

0 commit comments

Comments
 (0)