Skip to content

Commit

Permalink
Add sum-of-multiples exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Oct 27, 2024
1 parent 77f47cd commit 8899c11
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "sum-of-multiples",
"name": "Sum of Multiples",
"uuid": "430eeef3-d721-4a84-9e06-8e71f695c0e9",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "triangle",
"name": "Triangle",
Expand Down
27 changes: 27 additions & 0 deletions exercises/practice/sum-of-multiples/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Instructions

Your task is to write the code that calculates the energy points that get awarded to players when they complete a level.

The points awarded depend on two things:

- The level (a number) that the player completed.
- The base value of each magical item collected by the player during that level.

The energy points are awarded according to the following rules:

1. For each magical item, take the base value and find all the multiples of that value that are less than the level number.
2. Combine the sets of numbers.
3. Remove any duplicates.
4. Calculate the sum of all the numbers that are left.

Let's look at an example:

**The player completed level 20 and found two magical items with base values of 3 and 5.**

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.

- Multiples of 3 less than 20: `{3, 6, 9, 12, 15, 18}`
- Multiples of 5 less than 20: `{5, 10, 15}`
- Combine the sets and remove duplicates: `{3, 5, 6, 9, 10, 12, 15, 18}`
- Sum the unique multiples: `3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 = 78`
- Therefore, the player earns **78** energy points for completing level 20 and finding the two magical items with base values of 3 and 5.
6 changes: 6 additions & 0 deletions exercises/practice/sum-of-multiples/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Introduction

You work for a company that makes an online, fantasy-survival game.

When a player finishes a level, they are awarded energy points.
The amount of energy awarded depends on which magical items the player found while exploring that level.
19 changes: 19 additions & 0 deletions exercises/practice/sum-of-multiples/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"sum-of-multiples.wren"
],
"test": [
"sum-of-multiples.spec.wren"
],
"example": [
".meta/proof.ci.wren"
]
},
"blurb": "Given a number, find the sum of all the multiples of particular numbers up to but not including that number.",
"source": "A variation on Problem 1 at Project Euler",
"source_url": "https://projecteuler.net/problem=1"
}
14 changes: 14 additions & 0 deletions exercises/practice/sum-of-multiples/.meta/proof.ci.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class SumOfMultiples {
static sum(factors, limit) {
var total = 0
for (multiple in 1...limit) {
for (factor in factors) {
if (multiple % factor == 0) {
total = total + multiple
break
}
}
}
return total
}
}
58 changes: 58 additions & 0 deletions exercises/practice/sum-of-multiples/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[54aaab5a-ce86-4edc-8b40-d3ab2400a279]
description = "no multiples within limit"

[361e4e50-c89b-4f60-95ef-5bc5c595490a]
description = "one factor has multiples within limit"

[e644e070-040e-4ae0-9910-93c69fc3f7ce]
description = "more than one multiple within limit"

[607d6eb9-535c-41ce-91b5-3a61da3fa57f]
description = "more than one factor with multiples within limit"

[f47e8209-c0c5-4786-b07b-dc273bf86b9b]
description = "each multiple is only counted once"

[28c4b267-c980-4054-93e9-07723db615ac]
description = "a much larger limit"

[09c4494d-ff2d-4e0f-8421-f5532821ee12]
description = "three factors"

[2d0d5faa-f177-4ad6-bde9-ebb865083751]
description = "factors not relatively prime"

[ece8f2e8-96aa-4166-bbb7-6ce71261e354]
description = "some pairs of factors relatively prime and some not"

[624fdade-6ffb-400e-8472-456a38c171c0]
description = "one factor is a multiple of another"

[949ee7eb-db51-479c-b5cb-4a22b40ac057]
description = "much larger factors"

[41093673-acbd-482c-ab80-d00a0cbedecd]
description = "all numbers are multiples of 1"

[1730453b-baaa-438e-a9c2-d754497b2a76]
description = "no factors means an empty sum"

[214a01e9-f4bf-45bb-80f1-1dce9fbb0310]
description = "the only multiple of 0 is 0"

[c423ae21-a0cb-4ec7-aeb1-32971af5b510]
description = "the factor 0 does not affect the sum of multiples of other factors"

[17053ba9-112f-4ac0-aadb-0519dd836342]
description = "solutions using include-exclude must extend to cardinality greater than 3"
21 changes: 21 additions & 0 deletions exercises/practice/sum-of-multiples/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions exercises/practice/sum-of-multiples/package.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "wren-package" for WrenPackage, Dependency
import "os" for Process

class Package is WrenPackage {
construct new() {}
name { "exercism/sum-of-multiples" }
dependencies {
return [
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
]
}
}

Package.new().default()
68 changes: 68 additions & 0 deletions exercises/practice/sum-of-multiples/sum-of-multiples.spec.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import "./sum-of-multiples" for SumOfMultiples
import "wren-testie/testie" for Testie, Expect

Testie.test("SumOfMultiples") { |do, skip|
do.test("no multiples within limit") {
Expect.value(SumOfMultiples.sum([3, 5], 1)).toEqual(0)
}

skip.test("one factor has multiples within limit") {
Expect.value(SumOfMultiples.sum([3, 5], 4)).toEqual(3)
}

skip.test("more than one multiple within limit") {
Expect.value(SumOfMultiples.sum([3], 7)).toEqual(9)
}

skip.test("more than one factor with multiples within limit") {
Expect.value(SumOfMultiples.sum([3, 5], 10)).toEqual(23)
}

skip.test("each multiple is only counted once") {
Expect.value(SumOfMultiples.sum([3, 5], 100)).toEqual(2318)
}

skip.test("a much larger limit") {
Expect.value(SumOfMultiples.sum([3, 5], 1000)).toEqual(233168)
}

skip.test("three factors") {
Expect.value(SumOfMultiples.sum([7, 13, 17], 20)).toEqual(51)
}

skip.test("factors not relatively prime") {
Expect.value(SumOfMultiples.sum([4, 6], 15)).toEqual(30)
}

skip.test("some pairs of factors relatively prime and some not") {
Expect.value(SumOfMultiples.sum([5, 6, 8], 150)).toEqual(4419)
}

skip.test("one factor is a multiple of another") {
Expect.value(SumOfMultiples.sum([5, 25], 51)).toEqual(275)
}

skip.test("much larger factors") {
Expect.value(SumOfMultiples.sum([43, 47], 10000)).toEqual(2203160)
}

skip.test("all numbers are multiples of 1") {
Expect.value(SumOfMultiples.sum([1], 100)).toEqual(4950)
}

skip.test("no factors means an empty sum") {
Expect.value(SumOfMultiples.sum([], 10000)).toEqual(0)
}

skip.test("the only multiple of 0 is 0") {
Expect.value(SumOfMultiples.sum([0], 1)).toEqual(0)
}

skip.test("the factor 0 does not affect the sum of multiples of other factors") {
Expect.value(SumOfMultiples.sum([3, 0], 4)).toEqual(3)
}

skip.test("solutions using include-exclude must extend to cardinality greater than 3") {
Expect.value(SumOfMultiples.sum([2, 3, 5, 7, 11], 10000)).toEqual(39614537)
}
}
5 changes: 5 additions & 0 deletions exercises/practice/sum-of-multiples/sum-of-multiples.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class SumOfMultiples {
static sum(factors, limit) {
Fiber.abort("Remove this statement and implement this function")
}
}

0 comments on commit 8899c11

Please sign in to comment.