Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Create proof-of-concept
Browse files Browse the repository at this point in the history
  • Loading branch information
langsamu committed Jun 9, 2020
1 parent 753ec18 commit 206aca6
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
25 changes: 25 additions & 0 deletions AvastRecruitmentPrimeMultiplication.sln
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
41 changes: 41 additions & 0 deletions UnitTestProject1/Extensions.cs
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;
}
}

}
50 changes: 50 additions & 0 deletions UnitTestProject1/UnitTest1.cs
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);
}
}
}
}
15 changes: 15 additions & 0 deletions UnitTestProject1/UnitTestProject1.csproj
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>

0 comments on commit 206aca6

Please sign in to comment.