Skip to content

Commit ff59c35

Browse files
authored
Add trial division factorization algorithm (#133)
* Add trial division factorization algorithm * Add trial factorization to README.md
1 parent 6243e85 commit ff59c35

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Algorithms.Numeric.Factorization;
2+
using NUnit.Framework;
3+
4+
namespace Algorithms.Tests.Numeric
5+
{
6+
public static class TrialDivisionFactorizerTests
7+
{
8+
[Test]
9+
[TestCase(2)]
10+
[TestCase(3)]
11+
[TestCase(29)]
12+
[TestCase(31)]
13+
public static void PrimeNumberFactorizationFails(int p)
14+
{
15+
// Arrange
16+
var factorizer = new TrialDivisionFactorizer();
17+
18+
// Act
19+
var success = factorizer.TryFactor(p, out _);
20+
21+
// Assert
22+
Assert.IsFalse(success);
23+
}
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Algorithms.Numeric.Factorization
2+
{
3+
/// <summary>
4+
/// Finds a factor of a given number or returns false if it's prime.
5+
/// </summary>
6+
public interface IFactorizer
7+
{
8+
/// <summary>
9+
/// Finds a factor of a given number or returns false if it's prime.
10+
/// </summary>
11+
/// <param name="n">Integer to factor.</param>s
12+
/// <param name="factor">Found factor.</param>
13+
/// <returns><see langword="true"/> if factor is found, <see langword="false"/> if <paramref name="n"/> is prime.</returns>
14+
bool TryFactor(int n, out int factor);
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace Algorithms.Numeric.Factorization
5+
{
6+
/// <summary>
7+
/// Factors number using trial division algorithm.
8+
/// </summary>
9+
public class TrialDivisionFactorizer : IFactorizer
10+
{
11+
/// <summary>
12+
/// Finds a factor of a given number or returns false if it's prime.
13+
/// </summary>
14+
/// <param name="n">Integer to factor.</param>s
15+
/// <param name="factor">Found factor.</param>
16+
/// <returns><see langword="true"/> if factor is found, <see langword="false"/> if <paramref name="n"/> is prime.</returns>
17+
public bool TryFactor(int n, out int factor)
18+
{
19+
n = Math.Abs(n);
20+
factor = Enumerable.Range(2, (int)Math.Sqrt(n) - 1).FirstOrDefault(i => n % i == 0);
21+
return factor != 0;
22+
}
23+
}
24+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This repository contains algorithms and data structures implemented in C# for ed
2424
* [Greatest Common Divisor](./Algorithms/Numeric/GreatestCommonDivisor)
2525
* [Euclidean GCD](./Algorithms/Numeric/GreatestCommonDivisor/EuclideanGreatestCommonDivisorFinder.cs)
2626
* [Binary GCD](./Algorithms/Numeric/GreatestCommonDivisor/BinaryGreatestCommonDivisorFinder.cs)
27+
* [Factorization](./Algorithms/Numeric/Factorization)
28+
* [Trial division](./Algorithms/Numeric/Factorization/TrialDivisionFactorizer.cs)
2729
* [Gauss-Jordan Elimination](./Algorithms/Numeric/GaussJordanElimination.cs)
2830
* [Searches](./Algorithms/Search/)
2931
* [A-Star](./Algorithms/Search/AStar/)

0 commit comments

Comments
 (0)