mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-06 01:14:56 -06:00
PowerShell - sum-of-multiples
This commit is contained in:
22
powershell/sum-of-multiples/.exercism/config.json
Normal file
22
powershell/sum-of-multiples/.exercism/config.json
Normal 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"
|
||||
}
|
||||
1
powershell/sum-of-multiples/.exercism/metadata.json
Normal file
1
powershell/sum-of-multiples/.exercism/metadata.json
Normal 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}
|
||||
1
powershell/sum-of-multiples/.version
Normal file
1
powershell/sum-of-multiples/.version
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
33
powershell/sum-of-multiples/HELP.md
Normal file
33
powershell/sum-of-multiples/HELP.md
Normal 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/
|
||||
28
powershell/sum-of-multiples/README.md
Normal file
28
powershell/sum-of-multiples/README.md
Normal 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
|
||||
36
powershell/sum-of-multiples/SumOfMultiples.ps1
Normal file
36
powershell/sum-of-multiples/SumOfMultiples.ps1
Normal 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
|
||||
}
|
||||
29
powershell/sum-of-multiples/SumOfMultiples.tests.ps1
Normal file
29
powershell/sum-of-multiples/SumOfMultiples.tests.ps1
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user