Skip to content

Commit 2fd6eeb

Browse files
BNAndraskotp
andauthored
Add sum-of-multiples (#321)
Includes: * EOL at end of file --------- Co-authored-by: Victor Goff <[email protected]>
1 parent 694bf71 commit 2fd6eeb

File tree

8 files changed

+245
-0
lines changed

8 files changed

+245
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,14 @@
516516
"prerequisites": [],
517517
"difficulty": 3
518518
},
519+
{
520+
"slug": "sum-of-multiples",
521+
"name": "Sum of Multiples",
522+
"uuid": "fd01910f-030d-4d77-b7bc-019bd29b64ae",
523+
"practices": [],
524+
"prerequisites": [],
525+
"difficulty": 2
526+
},
519527
{
520528
"slug": "triangle",
521529
"name": "Triangle",
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.
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.
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.vim"
8+
],
9+
"test": [
10+
"sum_of_multiples.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function! Sum(factors, limit) abort
2+
let l:multiples = []
3+
for l:i in range(1, a:limit - 1)
4+
for l:factor in a:factors
5+
if l:i % l:factor == 0 && l:factor != 0
6+
call add(l:multiples, l:i)
7+
break
8+
endif
9+
endfor
10+
endfor
11+
12+
let l:sum = 0
13+
for l:multiple in l:multiples
14+
let l:sum += l:multiple
15+
endfor
16+
17+
return l:sum
18+
endfunction
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Execute (no multiples within limit):
2+
let g:factors = [3, 5]
3+
let g:limit = 1
4+
let g:expected = 0
5+
AssertEqual g:expected, Sum(g:factors, g:limit)
6+
7+
Execute (one factor has multiples within limit):
8+
let g:factors = [3, 5]
9+
let g:limit = 4
10+
let g:expected = 3
11+
AssertEqual g:expected, Sum(g:factors, g:limit)
12+
13+
Execute (more than one multiple within limit):
14+
let g:factors = [3]
15+
let g:limit = 7
16+
let g:expected = 9
17+
AssertEqual g:expected, Sum(g:factors, g:limit)
18+
19+
Execute (more than one factor with multiples within limit):
20+
let g:factors = [3, 5]
21+
let g:limit = 10
22+
let g:expected = 23
23+
AssertEqual g:expected, Sum(g:factors, g:limit)
24+
25+
Execute (each multiple is only counted once):
26+
let g:factors = [3, 5]
27+
let g:limit = 100
28+
let g:expected = 2318
29+
AssertEqual g:expected, Sum(g:factors, g:limit)
30+
31+
Execute (a much larger limit):
32+
let g:factors = [3, 5]
33+
let g:limit = 1000
34+
let g:expected = 233168
35+
AssertEqual g:expected, Sum(g:factors, g:limit)
36+
37+
Execute (three factors):
38+
let g:factors = [7, 13, 17]
39+
let g:limit = 20
40+
let g:expected = 51
41+
AssertEqual g:expected, Sum(g:factors, g:limit)
42+
43+
Execute (factors not relatively prime):
44+
let g:factors = [4, 6]
45+
let g:limit = 15
46+
let g:expected = 30
47+
AssertEqual g:expected, Sum(g:factors, g:limit)
48+
49+
Execute (some pairs of factors relatively prime and some not):
50+
let g:factors = [5, 6, 8]
51+
let g:limit = 150
52+
let g:expected = 4419
53+
AssertEqual g:expected, Sum(g:factors, g:limit)
54+
55+
Execute (one factor is a multiple of another):
56+
let g:factors = [5, 25]
57+
let g:limit = 51
58+
let g:expected = 275
59+
AssertEqual g:expected, Sum(g:factors, g:limit)
60+
61+
Execute (much larger factors):
62+
let g:factors = [43, 47]
63+
let g:limit = 10000
64+
let g:expected = 2203160
65+
AssertEqual g:expected, Sum(g:factors, g:limit)
66+
67+
Execute (all numbers are multiples of 1):
68+
let g:factors = [1]
69+
let g:limit = 100
70+
let g:expected = 4950
71+
AssertEqual g:expected, Sum(g:factors, g:limit)
72+
73+
Execute (no factors means an empty sum):
74+
let g:factors = []
75+
let g:limit = 10000
76+
let g:expected = 0
77+
AssertEqual g:expected, Sum(g:factors, g:limit)
78+
79+
Execute (the only multiple of 0 is 0):
80+
let g:factors = [0]
81+
let g:limit = 1
82+
let g:expected = 0
83+
AssertEqual g:expected, Sum(g:factors, g:limit)
84+
85+
Execute (the factor 0 does not affect the sum of multiples of other factors):
86+
let g:factors = [3, 0]
87+
let g:limit = 4
88+
let g:expected = 3
89+
AssertEqual g:expected, Sum(g:factors, g:limit)
90+
91+
Execute (solutions using include-exclude must extend to cardinality greater than 3):
92+
let g:factors = [2, 3, 5, 7, 11]
93+
let g:limit = 10000
94+
let g:expected = 39614537
95+
AssertEqual g:expected, Sum(g:factors, g:limit)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"
2+
" Given a number, find the sum of all the unique multiples
3+
" up to but not including that number.
4+
"
5+
" Examples:
6+
" :echo Sum([3, 5], 4)
7+
" 3
8+
"
9+
" :echo Sum([2, 3, 5, 7, 11], 10000)
10+
" 39614537
11+
"
12+
function! Sum(factors, limit) abort
13+
" your code goes here
14+
endfunction

0 commit comments

Comments
 (0)