PowerShell - sum-of-multiples

This commit is contained in:
Xevion
2021-11-26 02:30:29 -06:00
parent b6f26a5279
commit 4b5964861e
7 changed files with 150 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
{
"blurb": "Given a number, find the sum of all the multiples of particular numbers up to but not including that number.",
"authors": [
"gyssels"
],
"contributors": [
"kchenery"
],
"files": {
"solution": [
"SumOfMultiples.ps1"
],
"test": [
"SumOfMultiples.tests.ps1"
],
"example": [
".meta/SumOfMultiples.example.ps1"
]
},
"source": "A variation on Problem 1 at Project Euler",
"source_url": "http://projecteuler.net/problem=1"
}

View File

@@ -0,0 +1 @@
{"track":"powershell","exercise":"sum-of-multiples","id":"e24485af466b4525bced020dc247d87c","url":"https://exercism.org/tracks/powershell/exercises/sum-of-multiples","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,33 @@
# Help
## Running the tests
To run the tests run the command `Invoke-Pester` from within the exercise directory.
## Submitting your solution
You can submit your solution using the `exercism submit SumOfMultiples.ps1` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [PowerShell track's documentation](https://exercism.org/docs/tracks/powershell)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you are having trouble, you can use one of the following resources:
- [Powershell Documentation][powershell docs]
[Add more resources]: TODO
[powershell docs]: https://docs.microsoft.com/en-us/powershell/

View File

@@ -0,0 +1,28 @@
# Sum Of Multiples
Welcome to Sum Of Multiples on Exercism's PowerShell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Given a number, find the sum of all the unique multiples of particular numbers up to
but not including that number.
If we list all the natural numbers below 20 that are multiples of 3 or 5,
we get 3, 5, 6, 9, 10, 12, 15, and 18.
The sum of these multiples is 78.
## Source
### Created by
- @gyssels
### Contributed to by
- @kchenery
### Based on
A variation on Problem 1 at Project Euler - http://projecteuler.net/problem=1

View File

@@ -0,0 +1,36 @@
Function Get-SumOfMultiples {
<#
.SYNOPSIS
Given a number, find the sum of all the unique multiples of particular numbers up to
but not including that number.
.DESCRIPTION
If we list all the natural numbers below 20 that are multiples of 3 or 5,
we get 3, 5, 6, 9, 10, 12, 15, and 18.
.PARAMETER Multiples
An array of the factors
.PARAMETER Limit
The value BELOW which we test for
.EXAMPLE
Get-SumOfMultiples -Multiples @(3, 5) -Limit 10
Returns 23
#>
[CmdletBinding()]
Param(
[int[]]$Multiples,
[int]$Limit
)
$set = @{}
foreach ($Multiple in $Multiples) {
if ($Multiple -gt $Limit) { continue }
[int] $Maximum = [Math]::Floor($Limit / $Multiple)
if ($Multiple * $Maximum -ge $Limit) { $Maximum -= 1 }
1..$Maximum | % { $set[$_ * $Multiple] = $true }
}
return ($set.Keys | Measure-Object -Sum).Sum
}

View File

@@ -0,0 +1,29 @@
BeforeAll {
. ".\SumOfMultiples.ps1"
}
Describe "Get-SumOfMultiples Tests" {
It "Given the factors <Multiples> and to a limit of <Limit> the sum of multiples should be <ExpectedResult>" -TestCases @(
@{ Multiples = @(3, 5); Limit = 1; ExpectedResult = 0 },
@{ Multiples = @(3, 5); Limit = 4; ExpectedResult = 3 },
@{ Multiples = @(3); Limit = 7; ExpectedResult = 9 },
@{ Multiples = @(3, 5); Limit = 10; ExpectedResult = 23 },
@{ Multiples = @(3, 5); Limit = 100; ExpectedResult = 2318 },
@{ Multiples = @(3, 5); Limit = 1000; ExpectedResult = 233168 },
@{ Multiples = @(7, 13, 17); Limit = 20; ExpectedResult = 51 },
@{ Multiples = @(4, 6); Limit = 15; ExpectedResult = 30 },
@{ Multiples = @(5, 6, 8); Limit = 150; ExpectedResult = 4419 },
@{ Multiples = @(5, 25); Limit = 51; ExpectedResult = 275 },
@{ Multiples = @(43, 47); Limit = 10000; ExpectedResult = 2203160 },
@{ Multiples = @(1); Limit = 100; ExpectedResult = 4950 },
@{ Multiples = @(); Limit = 1000; ExpectedResult = 0 }
) {
Param(
[int[]]$Multiples,
[int]$Limit,
[int]$ExpectedResult
)
Get-SumOfMultiples -Multiples $Multiples -Limit $Limit | Should -BeExactly $ExpectedResult
}
}