Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FactorialCalculator implementation improved to handle large factorials #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/HitF5.Factorial.Tests/HitF5.Factorial.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="Shouldly" Version="4.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HitF5.Factorial\HitF5.Factorial.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions src/HitF5.Factorial.Tests/when_calculating_factorial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Numerics;
using NUnit.Framework;
using Shouldly;

namespace HitF5.Factorial.Tests
{
[TestFixture]
public class when_calculating_factorial
{
protected static IEnumerable<TestCaseData> FactorialIsCalculatedCorrectlyTestCases()
{
yield return new TestCaseData(0, new BigInteger(1))
.SetName("0");
yield return new TestCaseData(1, new BigInteger(1))
.SetName("1");
yield return new TestCaseData(2, new BigInteger(2))
.SetName("2");
yield return new TestCaseData(3, new BigInteger(6))
.SetName("3");
yield return new TestCaseData(4, new BigInteger(24))
.SetName("4");
yield return new TestCaseData(50, BigInteger.Parse("30414093201713378043612608166064768844377641568960512000000000000"))
.SetName("50");
}
[TestCaseSource(nameof(FactorialIsCalculatedCorrectlyTestCases))]
public void factorial_is_calculated_correctly(int value, BigInteger expectedFactorialValue)
{
var factorial = new FactorialCalculator().Calculate(value);

factorial.ShouldBe(expectedFactorialValue);
}

[Test]
public void exception_is_thrown_for_negative_value()
{
var ex = Should.Throw<ArgumentOutOfRangeException>(() => new FactorialCalculator().Calculate(-1));

ex.Message.ShouldBe("Value has to be non-negative. (Parameter 'value')");
}
}
}
30 changes: 9 additions & 21 deletions src/HitF5.Factorial/FactorialCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;

namespace HitF5.Factorial
{
[DebuggerDisplay("FactorialCalculator s minimem {minimum}.")]
public class FactorialCalculator : IFactorialCalculator
{
private static readonly int minimum = 1;

public int Calculate(int value)
public BigInteger Calculate(int value)
{
if (value <= minimum)
{
return minimum;
}
else
if (value < 0) throw new ArgumentOutOfRangeException(nameof(value), "Value has to be non-negative.");

var bi = new BigInteger(1);
var factorial = value;
for (var i = 1; i <= factorial; i++)
{
return value * this.Calculate(this.GetNext(value));
bi *= i;
}
}

private int GetNext(int value)
{
return value - 1;
}

public override string ToString()
{
return "ToString";
return bi;
}
}
}
6 changes: 4 additions & 2 deletions src/HitF5.Factorial/IFactorialCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace HitF5.Factorial
using System.Numerics;

namespace HitF5.Factorial
{
public interface IFactorialCalculator
{
int Calculate(int value);
BigInteger Calculate(int value);
}
}
19 changes: 15 additions & 4 deletions src/HitF5.sln
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29020.237
# Visual Studio Version 17
VisualStudioVersion = 17.3.32922.545
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HitF5.Random", "HitF5.Random\HitF5.Random.csproj", "{F45D20CB-4922-4255-BA12-DD34D2049BCE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HitF5.Custom", "HitF5.Custom\HitF5.Custom.csproj", "{A41E9F29-CEED-4DAA-A63F-14227B5A20D4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HitF5.Factorial", "HitF5.Factorial\HitF5.Factorial.csproj", "{338903E4-54D9-4443-A5C8-B27130BAD0FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HitF5.Api", "HitF5.Api\HitF5.Api.csproj", "{0750FB3B-BFFD-451E-BA6C-954044A735D0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HitF5.Api", "HitF5.Api\HitF5.Api.csproj", "{0750FB3B-BFFD-451E-BA6C-954044A735D0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HitF5.Api.Client", "HitF5.Api.Client\HitF5.Api.Client.csproj", "{4D6833CC-F7E9-4791-A371-FA40B32B0C37}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HitF5.Api.Client", "HitF5.Api.Client\HitF5.Api.Client.csproj", "{4D6833CC-F7E9-4791-A371-FA40B32B0C37}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{516F625A-3EDB-4B85-96F0-F20B3010815E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HitF5.Factorial.Tests", "HitF5.Factorial.Tests\HitF5.Factorial.Tests.csproj", "{5D2DDF72-EEC2-48B0-BDB0-33DCBDC5BA27}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -39,10 +43,17 @@ Global
{4D6833CC-F7E9-4791-A371-FA40B32B0C37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D6833CC-F7E9-4791-A371-FA40B32B0C37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D6833CC-F7E9-4791-A371-FA40B32B0C37}.Release|Any CPU.Build.0 = Release|Any CPU
{5D2DDF72-EEC2-48B0-BDB0-33DCBDC5BA27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D2DDF72-EEC2-48B0-BDB0-33DCBDC5BA27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D2DDF72-EEC2-48B0-BDB0-33DCBDC5BA27}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D2DDF72-EEC2-48B0-BDB0-33DCBDC5BA27}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5D2DDF72-EEC2-48B0-BDB0-33DCBDC5BA27} = {516F625A-3EDB-4B85-96F0-F20B3010815E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {51A73852-7393-4087-87E4-17E1AB8490A3}
EndGlobalSection
Expand Down