This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
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
4 changed files
with
131 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,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.30128.74 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject1", "UnitTestProject1\UnitTestProject1.csproj", "{2C1DA99E-D388-484C-A81B-07C14664C950}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{2C1DA99E-D388-484C-A81B-07C14664C950}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{2C1DA99E-D388-484C-A81B-07C14664C950}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{2C1DA99E-D388-484C-A81B-07C14664C950}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{2C1DA99E-D388-484C-A81B-07C14664C950}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {3F350A24-D07F-4172-8EE7-25461400CE51} | ||
EndGlobalSection | ||
EndGlobal |
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,41 @@ | ||
namespace UnitTestProject1 | ||
{ | ||
using System.Collections.Generic; | ||
|
||
internal static class Extensions | ||
{ | ||
internal static IEnumerable<int> Primes(this int count) | ||
{ | ||
var n = 1; // will start at two anyway | ||
|
||
// Decrement count until zero | ||
while (count-- > 0) | ||
{ | ||
// Increment number until prime | ||
while (!(++n).IsPrime()) | ||
{ | ||
} | ||
|
||
yield return n; | ||
} | ||
} | ||
|
||
internal static bool IsPrime(this int n) | ||
{ | ||
var i = 2; // one divides all | ||
|
||
// Check up to square root of n | ||
while (i * i <= n) | ||
{ | ||
// Check if divides and increment | ||
if (n % i++ == 0) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
} |
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,50 @@ | ||
namespace UnitTestProject1 | ||
{ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
public class UnitTest1 | ||
{ | ||
[TestMethod] | ||
public void ProofOfConcept() | ||
{ | ||
const int n = 10; | ||
var primes = n.Primes().ToArray(); | ||
|
||
var largestPrime = primes.Last(); | ||
var largestMultiple = largestPrime * largestPrime; | ||
var cellWidth = largestMultiple.ToString(CultureInfo.InvariantCulture).Length + 1; | ||
var format = $"{{0, {cellWidth}}}"; // Padding composite formatting string | ||
|
||
// Header row | ||
WriteRow(); | ||
|
||
// Body | ||
foreach (var prime in primes) | ||
{ | ||
WriteRow(prime); | ||
} | ||
|
||
void WriteRow(int? rowHeader = null) | ||
{ | ||
// Header column | ||
WriteCell(rowHeader); | ||
|
||
foreach (var prime in primes) | ||
{ | ||
WriteCell(prime, rowHeader ?? 1); | ||
} | ||
|
||
Console.WriteLine(); // LF | ||
} | ||
|
||
void WriteCell(int? prime1 = null, int? prime2 = 1) | ||
{ | ||
Console.Write(format, prime1 * prime2); | ||
} | ||
} | ||
} | ||
} |
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |