File tree Expand file tree Collapse file tree 4 files changed +67
-0
lines changed
Algorithms.Tests/Numeric/Factorization
Algorithms/Numeric/Factorization Expand file tree Collapse file tree 4 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -24,6 +24,8 @@ This repository contains algorithms and data structures implemented in C# for ed
24
24
* [Greatest Common Divisor](./Algorithms/Numeric/GreatestCommonDivisor)
25
25
* [Euclidean GCD](./Algorithms/Numeric/GreatestCommonDivisor/EuclideanGreatestCommonDivisorFinder.cs)
26
26
* [Binary GCD](./Algorithms/Numeric/GreatestCommonDivisor/BinaryGreatestCommonDivisorFinder.cs)
27
+ * [Factorization](./Algorithms/Numeric/Factorization)
28
+ * [Trial division](./Algorithms/Numeric/Factorization/TrialDivisionFactorizer.cs)
27
29
* [Gauss-Jordan Elimination](./Algorithms/Numeric/GaussJordanElimination.cs)
28
30
* [Searches](./Algorithms/Search/)
29
31
* [A-Star](./Algorithms/Search/AStar/)
You can’t perform that action at this time.
0 commit comments