Skip to content

Commit cd786d5

Browse files
n in ZZ and 2^n > n^2 is solved to a set, and a power's residues repeat with the base's order (#1449)
* n in ZZ and 2^n > n^2 is solved to a set: a window searched and its tails proved by induction An inequality with an exponential or a factorial over the whole numbers, conjoined with the membership, is searched over a window from the least member; the members are the points where it holds, and the whole numbers from the start of the final run -- and over ZZ down from the end of the first -- where the quantifier proves the statement there by induction. A tail it does not prove leaves the inequality solver's refusal in place: a window is evidence about the window. Sullivan and Mackey's Ex 5.3.2 is { 0, 1 } \/ ZZ /\ [5; +oo). The inequality induction reads the sign of the whole expression off its shape before looking for a step, which is what proves a downward tail. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * A power of a whole base repeats modulo m with the base's order, so the residues decide it forall n in ZZ+ : 3 divides 7^n - 4^n is one residue and forall n in ZZ* : not (7 divides 2^n + 1) three: a divisibility or a congruence in a polynomial of n and of powers a^e(n), a prime to the modulus and e a whole polynomial, has period lcm(m, ord_m(a), ...) in n over the non-negative whole numbers. A base sharing a factor with the modulus repeats only eventually, and over ZZ a power is fractional at a negative exponent; both are left as written. Sullivan and Mackey's §5.2.4 Try 3 and Ex 5.3.7. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 9b81213 commit cd786d5

8 files changed

Lines changed: 289 additions & 11 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,34 @@ written
788788
| `"forall n in ZZ+ : n! >= 2^(n - 1)".ToEntity().Evaled` | `UnhandledParseException` | `True` |
789789
| `"forall n in ZZ+ /\\ [4; +oo) : n^2 - 2 n - 1 > 0".ToEntity().Evaled` | `UnhandledParseException` | `True` — decided about `4 + t` over `ZZ*` |
790790

791+
### `n in ZZ and 2^n > n^2` is solved to a set, and was a refusal
792+
793+
An inequality with an exponential or a factorial over the whole numbers is solved to the members
794+
of a window from the least member and the rays the quantifier proves by induction, upwards and,
795+
over `ZZ`, downwards; a tail it does not prove leaves the refusal in place, since a window is
796+
evidence about the window ([#1409](https://github.com/asc-community/AngouriMath/issues/1409),
797+
Sullivan and Mackey's Ex 5.3.2 and Prob 5.7.2, 5.7.8–9).
798+
799+
| Input | Was (2.5.0) | Now |
800+
|---|---|---|
801+
| `"n in ZZ and 2^n > n^2".ToEntity().Solve("n")` | `NotSufficientlySupportedException` — "Only polynomial inequalities are supported" | `{ 0, 1 } \/ ZZ /\ [5; +oo)` |
802+
| `"n in ZZ+ and n! > 3^n".ToEntity().Solve("n")` | `NotSufficientlySupportedException` | `ZZ+ /\ [7; +oo)` |
803+
| `"n in ZZ and n^3 < 3^(n - 1)".ToEntity().Solve("n")` | `NotSufficientlySupportedException` | `ZZ /\ (-oo; 0] \/ ZZ /\ [6; +oo)` |
804+
| `"n in ZZ and 2^n >= n^2 - 3 n + 3".ToEntity().Solve("n")` | `NotSufficientlySupportedException` | the same — the tail from `1` is not proved, and the window is not guessed from |
805+
806+
And a divisibility or a congruence with a power of a whole base in the quantified name is decided
807+
by its residues too, over the non-negative whole numbers: `a^n` modulo `m` repeats with the order
808+
of `a` where `a` is prime to `m`, so `forall n in ZZ+ : 3 divides 7^n - 4^n` is `True` by one
809+
residue and `forall n in ZZ* : not (7 divides 2^n + 1)` by three (Sullivan and Mackey's §5.2.4 Try 3
810+
and Ex 5.3.7). A base sharing a factor with the modulus, or a negative exponent over `ZZ`, is left
811+
as written.
812+
813+
| Input | Was (2.5.0) | Now |
814+
|---|---|---|
815+
| `forall n in ZZ+ : 3 divides 7^n - 4^n` | `UnhandledParseException` | `True` |
816+
| `forall n in ZZ* : not (7 divides 2^n + 1)` | `UnhandledParseException` | `True` |
817+
| `forall n in ZZ+ : 5 divides 7^n - 1` | `UnhandledParseException` | `False` |
818+
791819
### `binomial(n, k)` is a function
792820

793821
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
291291
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
292292
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
293293

294+
[AngouriMath/Functions/Continuous/Solvers/InequalitySolver/ThresholdSearch.cs]
295+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
296+
297+
[Tests/UnitTests/Algebra/ThresholdSetTest.cs]
298+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
299+
294300
[Tests/UnitTests/Core/InductionTest.cs]
295301
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
296302

‎Sources/AngouriMath/Docs/Usage/Syntax.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ congruence is solved to a residue class — `"3 x = 11 (mod 7)".ToEntity().Solve
8585
Chinese remainder theorem, `x = 3 (mod 4) and x = 5 (mod 6)` giving `{ x in ZZ : x = 11 (mod 12) }`
8686
and `x = 3 (mod 4) and x = 2 (mod 6)` nothing, and with a bounded interval the members are listed:
8787
`x = 2 (mod 7) and 0 <= x and x < 30` is `{ 2, 9, 16, 23 }`. `MathS.NumberTheory.ModularInverse(3, 10)`
88-
is `7`, the one representative in `[1, n - 1]`, and `null` where `a` and `n` share a factor.
88+
is `7`, the one representative in `[1, n - 1]`, and `null` where `a` and `n` share a factor. An
89+
inequality with an exponential or a factorial over the whole numbers, `n in ZZ and 2^n > n^2`, is
90+
solved to the members of a window from the least member and the tails the quantifier proves by
91+
induction — `{ 0, 1 } \/ ZZ /\ [5; +oo)`, `n in ZZ+ and n! > 3^n` is `ZZ+ /\ [7; +oo)` — and where
92+
a tail is not proved the window is not an answer, and the inequality solver refuses as before.
8993
| 9 | `unite` `\/`, `setsubtract` `\` | one level, so `A \/ B \ C` is `(A \/ B) \ C` |
9094
| 10 | `intersect` `/\` | |
9195
| 11 | `+` `-` | |
@@ -317,7 +321,13 @@ factorial, a polynomial in `n` decided over the set, sums and products of these
317321
ZZ+ /\ [5; +oo) : 2^n > n^2` is `True` (`2^(n + 1) - 2 · 2^n` leaves `n^2 - 2 n - 1`, positive
318322
from `5`), `forall n in ZZ+ /\ [4; +oo) : 2^n > n^2` is `False` at `4`, and `forall n in ZZ+ :
319323
n! >= 2^(n - 1)` is `True`. The whole numbers from `m` are `ZZ*` shifted by `m`, so a statement
320-
over `ZZ+ /\ [4; +oo)` is decided as one about `4 + t` over `ZZ*`, whichever route reads it. `not`
324+
over `ZZ+ /\ [4; +oo)` is decided as one about `4 + t` over `ZZ*`, whichever route reads it. A
325+
divisibility or a congruence in a polynomial of `n` with whole coefficients repeats with the
326+
modulus, so its residues decide it (`forall n in ZZ : 6 divides n^3 + 5 n` is `True` by six cases),
327+
and with a power `a^n` of a whole base prime to the modulus among the terms it repeats with the
328+
order of `a` as well: `forall n in ZZ+ : 3 divides 7^n - 4^n` is `True` by one residue, `forall n
329+
in ZZ* : not (7 divides 2^n + 1)` by three — over the non-negative whole numbers, where the powers
330+
are whole; a base sharing a factor with the modulus repeats only eventually and is left as written. `not`
321331
passes through a quantifier by flipping it: `not forall x in S : P` is `exists x in S : not P`. In LaTeX, `\forall x \in S : P`. `forall`, `exists` and
322332
`exists!` are keywords, so they are not names.
323333

‎Sources/AngouriMath/Functions/Boolean/Quantifiers.cs‎

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ internal enum Kind { All, Some, Unique }
128128
// whole coefficients repeats with the modulus, so the residues decide it:
129129
// forall n in ZZ : 6 divides n^3 + 5 n is six cases. Sullivan and Mackey's "case
130130
// analysis over the classes" (Ex 6.5.14).
131-
if (Period(body, x) is { } period && period.CompareTo(EInteger.FromInt32(LargestPeriod)) <= 0)
131+
if (Period(body, x, integers) is { } period && period.CompareTo(EInteger.FromInt32(LargestPeriod)) <= 0)
132132
return OverResidues(kind, x, integers, body, period, isExact);
133133
// A polynomial equation with no solution modulo some m has none in the whole
134134
// numbers: 3 x^2 - 5 y^2 = 1 is impossible modulo 5 (Ex 6.5.27). Only a refusal
@@ -156,7 +156,7 @@ internal enum Kind { All, Some, Unique }
156156
/// The least member of a set of whole numbers bounded below -- <c>ZZ+</c>, <c>ZZ*</c>,
157157
/// either cut by an interval -- or <see langword="null"/> where the set is something else.
158158
/// </summary>
159-
private static Integer? LeastMember(Set set)
159+
internal static Integer? LeastMember(Set set)
160160
{
161161
switch (set)
162162
{
@@ -282,6 +282,9 @@ private static bool IsUnboundedAbove(Intersectionf cut)
282282
return Entity.Boolean.False;
283283
if (atLeast != Entity.Boolean.True)
284284
return null;
285+
// Read off the shape first: 3^(-t - 1) + t^3 is positive on ZZ* term by term.
286+
if (Sign(positive, x, set, isExact) is { } direct && (!strict || direct == Signum.Positive))
287+
return Entity.Boolean.True;
285288
var next = positive.Substitute(x, x + Integer.One);
286289
foreach (var c in Multipliers(positive, x))
287290
{
@@ -448,7 +451,7 @@ private enum Signum { Positive, NonNegative }
448451
_ => null,
449452
};
450453

451-
private static bool IsIntegerSet(SpecialSet set)
454+
internal static bool IsIntegerSet(SpecialSet set)
452455
=> set.ToDomain() is Domain.Integer or Domain.NonNegativeInteger or Domain.PositiveInteger;
453456

454457
/// <summary>How many residues a periodic statement is checked over before it is left as written.</summary>
@@ -460,34 +463,74 @@ private static bool IsIntegerSet(SpecialSet set)
460463
/// with whole coefficients, joined by the connectives; the least common multiple of the
461464
/// parts' periods.
462465
/// </summary>
463-
private static EInteger? Period(Entity body, Variable x)
466+
private static EInteger? Period(Entity body, Variable x, SpecialSet set)
464467
{
465468
switch (body)
466469
{
467470
case Entity.Boolean:
468471
return EInteger.One;
469472
case Dividesf(Integer divisor, var dividend):
470-
return divisor.EInteger.IsZero || !IsWholePolynomial(dividend) ? null : divisor.EInteger.Abs();
473+
return divisor.EInteger.IsZero ? null : PeriodModulo(dividend, divisor.EInteger.Abs(), x, set);
471474
case Congruentf(var left, var right, Integer modulus):
472-
return modulus.EInteger.IsZero || !IsWholePolynomial(left) || !IsWholePolynomial(right) ? null : modulus.EInteger.Abs();
475+
return modulus.EInteger.IsZero ? null : PeriodModulo(left - right, modulus.EInteger.Abs(), x, set);
473476
case Notf(var operand):
474-
return Period(operand, x);
477+
return Period(operand, x, set);
475478
// A quantifier over whole numbers inside: its body is periodic in x with the
476479
// same period whatever the inner name takes, so the residues of x decide the
477480
// outer statement and the inner one is decided at each of them.
478481
case Quantifier { Over: SpecialSet inner } quantifier when IsIntegerSet(inner):
479-
return Period(quantifier.Body, x);
482+
return Period(quantifier.Body, x, set);
480483
case Andf or Orf or Impliesf or Xorf:
481484
if (body is not IBinaryNode { NodeFirstChild: var first, NodeSecondChild: var second })
482485
return null;
483-
if (Period(first, x) is not { } one || Period(second, x) is not { } another)
486+
if (Period(first, x, set) is not { } one || Period(second, x, set) is not { } another)
484487
return null;
485488
return one * another / one.Gcd(another);
486489
default:
487490
return body.ContainsNode(x) ? null : EInteger.One;
488491
}
489492
}
490493

494+
/// <summary>
495+
/// The period, in <paramref name="x"/>, of an expression's residue modulo
496+
/// <paramref name="modulus"/>: the modulus for a polynomial with whole coefficients,
497+
/// and with a power <c>a^e</c> of a whole base coprime to the modulus among the terms --
498+
/// <c>7^n - 4^n</c> modulo <c>3</c>, <c>2^n + 1</c> modulo <c>7</c> -- the least common
499+
/// multiple with the order of each base, since <c>a^(e + k) = a^e</c> modulo <c>m</c>
500+
/// where <c>a^k = 1</c>. Over the non-negative whole numbers only, where the powers are
501+
/// whole; <see langword="null"/> for a base sharing a factor with the modulus, whose
502+
/// powers repeat only eventually. Sullivan and Mackey's Ex 5.3.7 and §5.2.4 Try 3.
503+
/// </summary>
504+
private static EInteger? PeriodModulo(Entity expr, EInteger modulus, Variable x, SpecialSet set)
505+
{
506+
if (IsWholePolynomial(expr))
507+
return modulus;
508+
if (set.ToDomain() is not (Domain.NonNegativeInteger or Domain.PositiveInteger))
509+
return null;
510+
var period = modulus;
511+
var replaced = expr;
512+
var count = 0;
513+
foreach (var power in expr.Nodes.OfType<Powf>().Where(p => p.Exponent.ContainsNode(x)).Distinct())
514+
{
515+
if (power.Base.Evaled is not Integer { EInteger: var @base } || !power.Exponent.Vars.All(v => v == x) || !IsWholePolynomial(power.Exponent))
516+
return null;
517+
if (!@base.Gcd(modulus).Equals(EInteger.One))
518+
return null;
519+
var order = EInteger.One;
520+
var value = @base.Mod(modulus);
521+
while (!value.Equals(EInteger.One) && order.CompareTo(modulus) < 0)
522+
{
523+
value = value * @base % modulus;
524+
order += 1;
525+
}
526+
if (!value.Equals(EInteger.One))
527+
return null;
528+
period = period * order / period.Gcd(order);
529+
replaced = replaced.Substitute(power, Variable.CreateUnique(expr, "power_" + count++));
530+
}
531+
return IsWholePolynomial(replaced) ? period : null;
532+
}
533+
491534
/// <summary>
492535
/// A polynomial with whole coefficients in whatever names it mentions -- which is
493536
/// periodic in each of them modulo anything, as long as the others stand for whole

‎Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ internal static Set Solve(Entity expr, Variable x)
332332

333333
Equalsf => Empty,
334334

335+
// x in ZZ and an inequality with an exponential or a factorial in x: a window
336+
// searched and its tails proved by induction, so that { n in ZZ : 2^n > n^2 }
337+
// is {0, 1} \/ ZZ /\ [5; +oo) rather than a refusal from the inequality solver.
338+
// https://github.com/asc-community/AngouriMath/issues/1409
339+
Andf(Inf(var name, Set integers), ComparisonSign inequality) when name == x && ThresholdSearch.Solve(x, integers, inequality) is { } thresholds => thresholds,
340+
Andf(ComparisonSign inequality, Inf(var name, Set integers)) when name == x && ThresholdSearch.Solve(x, integers, inequality) is { } thresholds => thresholds,
335341
Andf(var left, var right) =>
336342
Conjunction(Solve(left, x), Solve(right, x), expr, x),
337343
Orf(var left, var right) =>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using AngouriMath.Functions.Boolean;
11+
using static AngouriMath.Entity;
12+
using static AngouriMath.Entity.Number;
13+
using static AngouriMath.Entity.Set;
14+
15+
namespace AngouriMath.Functions.Algebra.AnalyticalSolving
16+
{
17+
/// <summary>
18+
/// <c>{ n in ZZ : 2^n > n^2 }</c>, an inequality with an exponential or a factorial over
19+
/// the whole numbers, is searched over a window from the least member and its tails are
20+
/// proved: the members are the points of the window where it holds, and the whole numbers
21+
/// from the start of the final run where the quantifier decides it holds there -- by
22+
/// induction, which is how Sullivan and Mackey do it (Ex 5.3.2: <c>{0, 1} \/ ZZ /\ [5; +oo)</c>).
23+
/// Over <c>ZZ</c> the other tail is proved the same way, downwards. A tail the quantifier
24+
/// leaves undecided leaves the set as written: a window is evidence about the window.
25+
/// https://github.com/asc-community/AngouriMath/issues/1409
26+
/// </summary>
27+
internal static class ThresholdSearch
28+
{
29+
/// <summary>How far from the least member, or from zero, the window reaches.</summary>
30+
private const int Window = 64;
31+
32+
internal static Set? Solve(Variable x, Set integers, ComparisonSign inequality)
33+
{
34+
if (inequality is Equalsf || inequality.Vars.Any(v => v != x))
35+
return null;
36+
if (!inequality.Nodes.Any(node => node is Powf(_, var e) && e.ContainsNode(x) || node is Factorialf f && f.ContainsNode(x)))
37+
return null;
38+
if (integers is not SpecialSet special || !Quantifiers.IsIntegerSet(special))
39+
return null;
40+
var least = Quantifiers.LeastMember(special);
41+
var from = least is null ? -Window : least.EInteger.ToInt32Checked();
42+
var truth = new List<bool>();
43+
for (var n = from; n <= Window; n++)
44+
{
45+
if (inequality.Substitute(x, Integer.Create(n)).Evaled is not Entity.Boolean(var holds))
46+
return null;
47+
truth.Add(holds);
48+
}
49+
// The final run, and the first one over ZZ, are what the quantifier is asked about.
50+
var last = truth.Count - 1;
51+
var tailStart = last;
52+
while (tailStart > 0 && truth[tailStart - 1] == truth[last])
53+
tailStart--;
54+
var upwards = truth[last] ? inequality : Negated(inequality);
55+
if (!Holds(upwards, x, from + tailStart, downwards: false))
56+
return null;
57+
var headEnd = 0;
58+
if (least is null)
59+
{
60+
while (headEnd < tailStart - 1 && truth[headEnd + 1] == truth[0])
61+
headEnd++;
62+
if (!Holds(truth[0] ? inequality : Negated(inequality), x, from + headEnd, downwards: true))
63+
return null;
64+
}
65+
// One run over the whole window, proved on every side, is the whole set.
66+
if (tailStart == 0 && truth[last])
67+
return special;
68+
if (tailStart == 0)
69+
return Empty;
70+
var listed = new List<Entity>();
71+
for (var i = least is null ? headEnd + 1 : 0; i < tailStart; i++)
72+
if (truth[i])
73+
listed.Add(Integer.Create(from + i));
74+
Set result = new FiniteSet(listed);
75+
if (least is null && truth[0])
76+
result = result.Unite(special.Intersect(new Interval(Real.NegativeInfinity, false, Integer.Create(from + headEnd), true)));
77+
if (truth[last])
78+
result = result.Unite(special.Intersect(new Interval(Integer.Create(from + tailStart), true, Real.PositiveInfinity, false)));
79+
return result;
80+
}
81+
82+
/// <summary>Whether the statement holds at every whole number from <paramref name="start"/> on, or down from it.</summary>
83+
private static bool Holds(Entity statement, Variable x, int start, bool downwards)
84+
{
85+
var t = Variable.CreateUnique(statement, "t");
86+
Entity shifted = downwards ? Integer.Create(start) - t : Integer.Create(start) + t;
87+
var body = statement.Substitute(x, shifted).InnerSimplified;
88+
return Quantifiers.Decide(Quantifiers.Kind.All, t, MathS.Sets.NonNegativeIntegers, body, true) is Entity.Boolean(true);
89+
}
90+
91+
private static ComparisonSign Negated(ComparisonSign inequality)
92+
=> inequality switch
93+
{
94+
Greaterf(var a, var b) => new LessOrEqualf(a, b),
95+
GreaterOrEqualf(var a, var b) => new Lessf(a, b),
96+
Lessf(var a, var b) => new GreaterOrEqualf(a, b),
97+
LessOrEqualf(var a, var b) => new Greaterf(a, b),
98+
_ => throw new Core.Exceptions.AngouriBugException("An equation is refused above"),
99+
};
100+
}
101+
}

0 commit comments

Comments
 (0)