Skip to content

Commit 8c0ec60

Browse files
MathS.NumberTheory.Factorize refuses 0 and negative integers (#2468)
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 <noreply@anthropic.com>
1 parent 3abd4d9 commit 8c0ec60

5 files changed

Lines changed: 83 additions & 7 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ The calculus nodes and the binders — `derivative`, `integral`, `limit`, `sum`,
6161
names the node and says to compile `.Evaled` or `Simplify()` instead. The LINQ compiler said the bound variable was not
6262
among the arguments.
6363

64+
### `MathS.NumberTheory.Factorize` refuses 0 and negative integers
65+
66+
**Not silent: a wrong answer now throws.** `Factorize` answered 0, -1 and -12 with the empty list, which is the
67+
factorisation of 1. 0 has no factorisation into primes, and a negative integer is -1 times the factorisation of its
68+
absolute value, which a list of primes cannot write. Each now throws `InvalidNumberException`, the type for a numeric
69+
argument an operation cannot use, as a negative term count for a Taylor expansion does. 1 is still the empty list. `phi`,
70+
`divisorsigma`, `mobius`, `primenu`, `primeomega`, `valuation` and `CountDivisors` do not go through it and are unchanged
71+
([#2466](https://github.com/asc-community/AngouriMath/issues/2466)).
72+
73+
| Input | Was (2.7.0) | Now |
74+
|---|---|---|
75+
| `MathS.NumberTheory.Factorize(0)` | `[]` | throws `InvalidNumberException` |
76+
| `MathS.NumberTheory.Factorize(-1)` | `[]` | throws `InvalidNumberException` |
77+
| `MathS.NumberTheory.Factorize(-12)` | `[]` | throws `InvalidNumberException` |
78+
| `MathS.NumberTheory.Factorize(1)` | `[]` | unchanged |
79+
| `MathS.NumberTheory.Factorize(360)` | `[(2, 3), (3, 2), (5, 1)]` | unchanged |
80+
6481
### A definite integral whose antiderivative splits into cases finds the breaks of each case
6582

6683
**Silent: a wrong value is corrected.** The antiderivative of `1/(t^2 + y^2)` is a case split on `y`, and its case

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,13 @@ public static class NumberTheory
179179
public static Integer CountDivisors(Integer integer) => integer.CountDivisors();
180180

181181
/// <summary>
182-
/// Factorization of integer. Returns a sequence of unique primes
183-
/// and their corresponding power. Sorted by value of prime (starting
184-
/// from the smallest).
182+
/// The prime factorisation of a positive integer: each prime that divides it with its
183+
/// power, the smallest prime first. Empty for 1, the empty product.
185184
/// </summary>
185+
/// <exception cref="InvalidNumberException">
186+
/// Thrown where <paramref name="integer"/> is 0 or negative, which have no factorisation
187+
/// into primes alone. https://github.com/asc-community/AngouriMath/issues/2466
188+
/// </exception>
186189
/// <example>
187190
/// <code>
188191
/// var factors = MathS.NumberTheory.Factorize(1872);

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,20 @@ public static Integer Create(EInteger value)
7676
public Integer Phi() => EInteger.Phi();
7777

7878
/// <summary>
79-
/// Factorization of integer
80-
/// </summary>
79+
/// The prime factorisation of a positive integer: each prime that divides it with its power, the
80+
/// smallest prime first. Empty for 1, the empty product.
81+
/// </summary>
82+
/// <exception cref="Core.Exceptions.InvalidNumberException">
83+
/// Thrown where the integer is 0 or negative. 0 has no factorisation into primes, and a negative
84+
/// integer factors as -1 times the factorisation of its absolute value, which a list of primes
85+
/// cannot write; so neither is answered as a list, which would claim the integer is 1 or its
86+
/// absolute value. https://github.com/asc-community/AngouriMath/issues/2466
87+
/// </exception>
8188
public IEnumerable<(Integer prime, Integer power)> Factorize() =>
82-
EInteger.Factorize().Select(x => ((Integer) x.prime, (Integer) x.power));
89+
EInteger.Sign > 0
90+
? EInteger.Factorize().Select(x => ((Integer) x.prime, (Integer) x.power))
91+
: throw new Core.Exceptions.InvalidNumberException(
92+
$"A prime factorisation is of a positive integer, and {EInteger} was given");
8393

8494
/// <summary>
8595
/// Count of all divisors of an integer

‎Sources/AngouriMath/Core/Operations/Catalogue.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ internal static class Catalogue
229229

230230
/// <summary><see cref="MathS.NumberTheory.Factorize(Integer)"/>.</summary>
231231
public static Query<Func<Integer, IEnumerable<(Integer Prime, Integer Power)>>> PrimeFactorization { get; } = new(
232-
"prime_factorization", "The primes that divide an integer, each with its power.",
232+
"prime_factorization", "The primes that divide a positive integer, each with its power.",
233233
static n => MathS.NumberTheory.Factorize(n));
234234

235235
/// <summary><see cref="MathS.NumberTheory.Phi(Entity)"/>, evaluated.</summary>

‎Sources/Tests/UnitTests/Common/AFactorisationPastTheLongsTest.cs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System;
99
using System.Linq;
1010
using System.Threading.Tasks;
11+
using AngouriMath.Core.Exceptions;
12+
using AngouriMath.Core.Operations;
1113
using AngouriMath.Extensions;
1214
using PeterO.Numbers;
1315
using Xunit;
@@ -80,6 +82,50 @@ public void APowerOfALargePrimeHasItsTotient()
8082
Within(() => "phi((2^89 - 1)^k)".ToEntity().Simplify()));
8183
}
8284

85+
/// <summary>
86+
/// An empty list is the factorisation of 1, so it answered 0, -1 and -12 as though each were 1.
87+
/// https://github.com/asc-community/AngouriMath/issues/2466
88+
/// </summary>
89+
[Theory]
90+
[InlineData("0")]
91+
[InlineData("-1")]
92+
[InlineData("-12")]
93+
[InlineData("-18446744073709551616")]
94+
public void Issue2466_ANumberThatIsNotPositiveIsRefused(string number)
95+
{
96+
var integer = Integer.Create(EInteger.FromString(number));
97+
Assert.Throws<InvalidNumberException>(() => MathS.NumberTheory.Factorize(integer));
98+
Assert.Throws<InvalidNumberException>(() => integer.Factorize());
99+
Assert.Throws<InvalidNumberException>(() => Catalogue.PrimeFactorization.Ask(integer));
100+
}
101+
102+
[Fact]
103+
public void Issue2466_OneIsTheEmptyProduct()
104+
=> Assert.Empty(MathS.NumberTheory.Factorize(Integer.One));
105+
106+
/// <summary>
107+
/// The functions of a factorisation do not ask <see cref="MathS.NumberTheory.Factorize"/>, and keep their
108+
/// own answers at 0 and below. https://github.com/asc-community/AngouriMath/issues/2466
109+
/// </summary>
110+
[Theory]
111+
[InlineData("phi(0)", "0")]
112+
[InlineData("phi(-12)", "0")]
113+
[InlineData("divisorsigma(1, -12)", "NaN")]
114+
[InlineData("divisorsigma(1, 0)", "NaN")]
115+
[InlineData("mobius(-12)", "NaN")]
116+
[InlineData("primenu(-12)", "NaN")]
117+
[InlineData("primeomega(0)", "NaN")]
118+
[InlineData("valuation(-12, 2)", "2")]
119+
[InlineData("valuation(0, 2)", "+oo")]
120+
public void Issue2466_TheFunctionsOfAFactorisationKeepTheirAnswers(string input, string expected)
121+
=> Assert.Equal(expected == "NaN" ? MathS.NaN : expected.ToEntity(), input.ToEntity().Evaled);
122+
123+
[Theory]
124+
[InlineData("0")]
125+
[InlineData("-12")]
126+
public void Issue2466_DivisorsOfANumberThatIsNotPositiveStillCountZero(string number)
127+
=> Assert.Equal(Integer.Zero, MathS.NumberTheory.CountDivisors(Integer.Create(EInteger.FromString(number))));
128+
83129
private static T Within<T>(Func<T> evaluation)
84130
{
85131
var task = Task.Factory.StartNew(evaluation, TaskCreationOptions.LongRunning);

0 commit comments

Comments
 (0)