Skip to content
New issue

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

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

Already on GitHub? # to your account

Catalan numbers #48

Merged
merged 1 commit into from
Oct 7, 2018
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FluentAssertions;
using Xunit;

namespace Algorithms.Tests.CatalanNumbersTests
{
public class CatalanNumbersTests
{
[Fact]
public void GetNumbers_Generate3rdCatalanNumber_Return5()
{
var expected = 5;

var number = CatalanNumberGenerator.GetNumberOn(3);

number.Should().Be(expected);
}

[Fact]
public void GetNumbers_Generate15rdCatalanNumber_Return9694845()
{
var expected = 9694845;

var number = CatalanNumberGenerator.GetNumberOn(15);

number.Should().Be(expected);
}

[Fact]
public void GetNumbers_Generate0rdCatalanNumber_Return1()
{
var expected = 0;

var number = CatalanNumberGenerator.GetNumberOn(0);

number.Should().Be(expected);
}

[Fact]
public void GetNumbers_GenerateNegativePos_Return0()
{
var expected = 0;

var number = CatalanNumberGenerator.GetNumberOn(-1);

number.Should().Be(expected);
}
}
}
26 changes: 26 additions & 0 deletions Algorithms/Algorithms/CatalanNumbers/CatalanNumberGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace Algorithms
{
public static class CatalanNumberGenerator
{
private static double Factorial(double n)
{
if (n <= 0.0)
return 1;

return n * Factorial(n - 1);
}

public static double GetNumberOn(int n)
{
if (n <= 0)
return 0;

const double topMultiplier = 2.0;
var result = Factorial(topMultiplier * n) / (Factorial(n + 1) * Factorial(n));

return Math.Ceiling(result);
}
}
}