-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace MultiplesOf3Or5 | ||
{ | ||
public class MultiplesOf3Or5 | ||
{ | ||
/* | ||
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. | ||
The sum of these multiples is 23. | ||
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. | ||
Additionally, if the number is negative, return 0 (for languages that do have them). | ||
Note: If the number is a multiple of both 3 and 5, only count it once. | ||
*/ | ||
public static int GetMultiplesOf3Or5(int num) | ||
{ | ||
if (num < 1) { return 0; } | ||
|
||
var result = 0; | ||
var i = 1; | ||
while (i < num) | ||
{ | ||
var isMultipleOf3 = i % 3 == 0; | ||
var isMultipleOf5 = i % 5 == 0; | ||
if (isMultipleOf3 && isMultipleOf5) | ||
{ | ||
result += i; | ||
i++; | ||
continue; | ||
} | ||
|
||
if (isMultipleOf3 || isMultipleOf5) | ||
{ | ||
result += i; | ||
} | ||
i++; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
21 changes: 21 additions & 0 deletions
21
MultipleOf3Or5/MultiplesOf3Or5.Tests/MultiplesOf3Or5.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="NUnit" Version="3.13.2" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MultipleOf3Or5\MultiplesOf3Or5.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
14 changes: 14 additions & 0 deletions
14
MultipleOf3Or5/MultiplesOf3Or5.Tests/MultiplesOf3Or5Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using NUnit.Framework; | ||
|
||
namespace MultiplesOf3Or5.Tests | ||
{ | ||
public class Tests | ||
{ | ||
[Test] | ||
public void Test_GetMultipleOf3Or5() | ||
{ | ||
var actual = MultiplesOf3Or5.GetMultiplesOf3Or5(10); | ||
Assert.That(actual, Is.EqualTo(23)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31912.275 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiplesOf3Or5", "MultipleOf3Or5\MultiplesOf3Or5.csproj", "{D51D69AB-B09F-4C99-A9AE-1F267A5CDD7D}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiplesOf3Or5.Tests", "MultiplesOf3Or5.Tests\MultiplesOf3Or5.Tests.csproj", "{820D0399-8B83-43D2-933E-0054542E9CBB}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{D51D69AB-B09F-4C99-A9AE-1F267A5CDD7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{D51D69AB-B09F-4C99-A9AE-1F267A5CDD7D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{D51D69AB-B09F-4C99-A9AE-1F267A5CDD7D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{D51D69AB-B09F-4C99-A9AE-1F267A5CDD7D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{820D0399-8B83-43D2-933E-0054542E9CBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{820D0399-8B83-43D2-933E-0054542E9CBB}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{820D0399-8B83-43D2-933E-0054542E9CBB}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{820D0399-8B83-43D2-933E-0054542E9CBB}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {FD698353-54D6-46A4-B7F0-CA25AA705E97} | ||
EndGlobalSection | ||
EndGlobal |