From 7ff4495e32ed03f257d8f34c12b737d15270997e Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 15 Nov 2026 23:53:02 +0000 Subject: [PATCH] MathS.NumberTheory.Factorize refuses 0 and negative integers Part of #2466 Factorize answered 0, -1 and -12 with the empty list, the factorisation of 1. 0 has no factorisation into primes, and a negative integer is -1 times the factorisation of its absolute value, which a list of primes cannot write. Each now throws InvalidNumberException, the type Exceptions.md gives for a numeric argument an operation cannot use (as TaylorExpansion does for a negative term count); 1 is still the empty product. The return type is unchanged. phi, divisorsigma, mobius, primenu, primeomega, valuation and CountDivisors do not go through Integer.Factorize and answer as before at 0 and below, pinned by tests; the rational-root divisors take the absolute value of a non-zero coefficient. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 17 +++++++ Sources/AngouriMath/Convenience/MathS.cs | 9 ++-- .../Entity.Continuous.Integer.Definition.cs | 16 +++++-- .../AngouriMath/Core/Operations/Catalogue.cs | 2 +- .../Common/AFactorisationPastTheLongsTest.cs | 46 +++++++++++++++++++ 5 files changed, 83 insertions(+), 7 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index d38cc9375..4ea4e9076 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -61,6 +61,23 @@ The calculus nodes and the binders — `derivative`, `integral`, `limit`, `sum`, names the node and says to compile `.Evaled` or `Simplify()` instead. The LINQ compiler said the bound variable was not among the arguments. +### `MathS.NumberTheory.Factorize` refuses 0 and negative integers + +**Not silent: a wrong answer now throws.** `Factorize` answered 0, -1 and -12 with the empty list, which is the +factorisation of 1. 0 has no factorisation into primes, and a negative integer is -1 times the factorisation of its +absolute value, which a list of primes cannot write. Each now throws `InvalidNumberException`, the type for a numeric +argument an operation cannot use, as a negative term count for a Taylor expansion does. 1 is still the empty list. `phi`, +`divisorsigma`, `mobius`, `primenu`, `primeomega`, `valuation` and `CountDivisors` do not go through it and are unchanged +([#2466](https://github.com/asc-community/AngouriMath/issues/2466)). + +| Input | Was (2.7.0) | Now | +|---|---|---| +| `MathS.NumberTheory.Factorize(0)` | `[]` | throws `InvalidNumberException` | +| `MathS.NumberTheory.Factorize(-1)` | `[]` | throws `InvalidNumberException` | +| `MathS.NumberTheory.Factorize(-12)` | `[]` | throws `InvalidNumberException` | +| `MathS.NumberTheory.Factorize(1)` | `[]` | unchanged | +| `MathS.NumberTheory.Factorize(360)` | `[(2, 3), (3, 2), (5, 1)]` | unchanged | + ### A definite integral whose antiderivative splits into cases finds the breaks of each case **Silent: a wrong value is corrected.** The antiderivative of `1/(t^2 + y^2)` is a case split on `y`, and its case diff --git a/Sources/AngouriMath/Convenience/MathS.cs b/Sources/AngouriMath/Convenience/MathS.cs index 4545cfbe5..0f403478b 100644 --- a/Sources/AngouriMath/Convenience/MathS.cs +++ b/Sources/AngouriMath/Convenience/MathS.cs @@ -179,10 +179,13 @@ public static class NumberTheory public static Integer CountDivisors(Integer integer) => integer.CountDivisors(); /// - /// Factorization of integer. Returns a sequence of unique primes - /// and their corresponding power. Sorted by value of prime (starting - /// from the smallest). + /// The prime factorisation of a positive integer: each prime that divides it with its + /// power, the smallest prime first. Empty for 1, the empty product. /// + /// + /// Thrown where is 0 or negative, which have no factorisation + /// into primes alone. https://github.com/asc-community/AngouriMath/issues/2466 + /// /// /// /// var factors = MathS.NumberTheory.Factorize(1872); diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs index 45e7f481f..d8046c226 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs @@ -76,10 +76,20 @@ public static Integer Create(EInteger value) public Integer Phi() => EInteger.Phi(); /// - /// Factorization of integer - /// + /// The prime factorisation of a positive integer: each prime that divides it with its power, the + /// smallest prime first. Empty for 1, the empty product. + /// + /// + /// Thrown where the integer is 0 or negative. 0 has no factorisation into primes, and a negative + /// integer factors as -1 times the factorisation of its absolute value, which a list of primes + /// cannot write; so neither is answered as a list, which would claim the integer is 1 or its + /// absolute value. https://github.com/asc-community/AngouriMath/issues/2466 + /// public IEnumerable<(Integer prime, Integer power)> Factorize() => - EInteger.Factorize().Select(x => ((Integer) x.prime, (Integer) x.power)); + EInteger.Sign > 0 + ? EInteger.Factorize().Select(x => ((Integer) x.prime, (Integer) x.power)) + : throw new Core.Exceptions.InvalidNumberException( + $"A prime factorisation is of a positive integer, and {EInteger} was given"); /// /// Count of all divisors of an integer diff --git a/Sources/AngouriMath/Core/Operations/Catalogue.cs b/Sources/AngouriMath/Core/Operations/Catalogue.cs index c0d4a06ee..c9ef59468 100644 --- a/Sources/AngouriMath/Core/Operations/Catalogue.cs +++ b/Sources/AngouriMath/Core/Operations/Catalogue.cs @@ -229,7 +229,7 @@ internal static class Catalogue /// . public static Query>> PrimeFactorization { get; } = new( - "prime_factorization", "The primes that divide an integer, each with its power.", + "prime_factorization", "The primes that divide a positive integer, each with its power.", static n => MathS.NumberTheory.Factorize(n)); /// , evaluated. diff --git a/Sources/Tests/UnitTests/Common/AFactorisationPastTheLongsTest.cs b/Sources/Tests/UnitTests/Common/AFactorisationPastTheLongsTest.cs index 465bb856a..fe3497a1a 100644 --- a/Sources/Tests/UnitTests/Common/AFactorisationPastTheLongsTest.cs +++ b/Sources/Tests/UnitTests/Common/AFactorisationPastTheLongsTest.cs @@ -8,6 +8,8 @@ using System; using System.Linq; using System.Threading.Tasks; +using AngouriMath.Core.Exceptions; +using AngouriMath.Core.Operations; using AngouriMath.Extensions; using PeterO.Numbers; using Xunit; @@ -80,6 +82,50 @@ public void APowerOfALargePrimeHasItsTotient() Within(() => "phi((2^89 - 1)^k)".ToEntity().Simplify())); } + /// + /// An empty list is the factorisation of 1, so it answered 0, -1 and -12 as though each were 1. + /// https://github.com/asc-community/AngouriMath/issues/2466 + /// + [Theory] + [InlineData("0")] + [InlineData("-1")] + [InlineData("-12")] + [InlineData("-18446744073709551616")] + public void Issue2466_ANumberThatIsNotPositiveIsRefused(string number) + { + var integer = Integer.Create(EInteger.FromString(number)); + Assert.Throws(() => MathS.NumberTheory.Factorize(integer)); + Assert.Throws(() => integer.Factorize()); + Assert.Throws(() => Catalogue.PrimeFactorization.Ask(integer)); + } + + [Fact] + public void Issue2466_OneIsTheEmptyProduct() + => Assert.Empty(MathS.NumberTheory.Factorize(Integer.One)); + + /// + /// The functions of a factorisation do not ask , and keep their + /// own answers at 0 and below. https://github.com/asc-community/AngouriMath/issues/2466 + /// + [Theory] + [InlineData("phi(0)", "0")] + [InlineData("phi(-12)", "0")] + [InlineData("divisorsigma(1, -12)", "NaN")] + [InlineData("divisorsigma(1, 0)", "NaN")] + [InlineData("mobius(-12)", "NaN")] + [InlineData("primenu(-12)", "NaN")] + [InlineData("primeomega(0)", "NaN")] + [InlineData("valuation(-12, 2)", "2")] + [InlineData("valuation(0, 2)", "+oo")] + public void Issue2466_TheFunctionsOfAFactorisationKeepTheirAnswers(string input, string expected) + => Assert.Equal(expected == "NaN" ? MathS.NaN : expected.ToEntity(), input.ToEntity().Evaled); + + [Theory] + [InlineData("0")] + [InlineData("-12")] + public void Issue2466_DivisorsOfANumberThatIsNotPositiveStillCountZero(string number) + => Assert.Equal(Integer.Zero, MathS.NumberTheory.CountDivisors(Integer.Create(EInteger.FromString(number)))); + private static T Within(Func evaluation) { var task = Task.Factory.StartNew(evaluation, TaskCreationOptions.LongRunning);