Skip to content

Commit 74394df

Browse files
Add sum of multiples exercise (#556)
1 parent a7e6aba commit 74394df

File tree

14 files changed

+214
-0
lines changed

14 files changed

+214
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,14 @@
622622
"practices": [],
623623
"prerequisites": [],
624624
"difficulty": 4
625+
},
626+
{
627+
"slug": "sum-of-multiples",
628+
"name": "Sum of Multiples",
629+
"uuid": "e127a984-ebb1-4709-9cc9-a740bceda94c",
630+
"practices": [],
631+
"prerequisites": [],
632+
"difficulty": 4
625633
}
626634
]
627635
},
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": ["therealowenrees"],
3+
"files": {
4+
"solution": [
5+
"sum_of_multiples.ml"
6+
],
7+
"test": [
8+
"test.ml"
9+
],
10+
"example": [
11+
".meta/example.ml"
12+
],
13+
"editor": [
14+
"sum_of_multiples.mli"
15+
]
16+
},
17+
"blurb": "Given a number, find the sum of all the multiples of particular numbers up to but not including that number.",
18+
"source": "A variation on Problem 1 at Project Euler",
19+
"source_url": "https://projecteuler.net/problem=1"
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let sum factors limit =
2+
List.init (limit - 1) ((+) 1)
3+
|> List.filter (fun i -> List.exists (fun f -> f <> 0 && i mod f = 0) factors)
4+
|> List.fold_left ( + ) 0
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
default: clean test
2+
3+
test:
4+
dune runtest
5+
6+
clean:
7+
dune clean
8+
9+
.PHONY: clean
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(executable
2+
(name test)
3+
(libraries base ounit2))
4+
5+
(alias
6+
(name runtest)
7+
(deps
8+
(:x test.exe))
9+
(action
10+
(run %{x})))
11+
12+
(alias
13+
(name buildtest)
14+
(deps
15+
(:x test.exe)))
16+
17+
(env
18+
(dev
19+
(flags
20+
(:standard -warn-error -A))))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let sum _ _ = failwith "'sum' not implemented"

0 commit comments

Comments
 (0)