diff --git a/powershell/sum-of-multiples/.exercism/config.json b/powershell/sum-of-multiples/.exercism/config.json new file mode 100644 index 0000000..bfb39dc --- /dev/null +++ b/powershell/sum-of-multiples/.exercism/config.json @@ -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" +} diff --git a/powershell/sum-of-multiples/.exercism/metadata.json b/powershell/sum-of-multiples/.exercism/metadata.json new file mode 100644 index 0000000..446a019 --- /dev/null +++ b/powershell/sum-of-multiples/.exercism/metadata.json @@ -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} \ No newline at end of file diff --git a/powershell/sum-of-multiples/.version b/powershell/sum-of-multiples/.version new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/powershell/sum-of-multiples/.version @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/powershell/sum-of-multiples/HELP.md b/powershell/sum-of-multiples/HELP.md new file mode 100644 index 0000000..661481c --- /dev/null +++ b/powershell/sum-of-multiples/HELP.md @@ -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/ \ No newline at end of file diff --git a/powershell/sum-of-multiples/README.md b/powershell/sum-of-multiples/README.md new file mode 100644 index 0000000..0be7ac6 --- /dev/null +++ b/powershell/sum-of-multiples/README.md @@ -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 \ No newline at end of file diff --git a/powershell/sum-of-multiples/SumOfMultiples.ps1 b/powershell/sum-of-multiples/SumOfMultiples.ps1 new file mode 100644 index 0000000..6e57294 --- /dev/null +++ b/powershell/sum-of-multiples/SumOfMultiples.ps1 @@ -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 +} \ No newline at end of file diff --git a/powershell/sum-of-multiples/SumOfMultiples.tests.ps1 b/powershell/sum-of-multiples/SumOfMultiples.tests.ps1 new file mode 100644 index 0000000..1d0bd06 --- /dev/null +++ b/powershell/sum-of-multiples/SumOfMultiples.tests.ps1 @@ -0,0 +1,29 @@ +BeforeAll { + . ".\SumOfMultiples.ps1" +} + +Describe "Get-SumOfMultiples Tests" { + It "Given the factors and to a limit of the sum of multiples should be " -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 + } +}