Skip to content

Commit d059edd

Browse files
Floors and ceilings of n over a whole number are decided over its residues (#1566)
A comparison between floors and ceilings of linear functions of n and a linear part repeats with the least common multiple of the floors' denominators where the drifts cancel: over a period, a floor of (p/q) n + b rises by as much as the linear terms do. The residues then decide it, as they decide a divisibility: floor(n/2) + ceil(n/2) = n and Hermite's identity for thirds hold for every whole n. Part of #1409. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c3fb4fc commit d059edd

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,20 @@ Every spelling but `lcm` was a parse error in 2.5.0, the congruence node being n
597597
| `forall n in ZZ : 6 divides n^3 + 5 n` | `UnhandledParseException` | `True` |
598598
| `exists x, y in ZZ : 3 x^2 - 5 y^2 = 1` | `UnhandledParseException` | `False` |
599599

600+
### Floors and ceilings of `n` over a whole number are decided over its residues
601+
602+
A comparison between floors and ceilings of linear functions of `n` and a linear part in `n`
603+
repeats with the least common multiple of their denominators where the drifts cancel: over a
604+
period, a floor of `(p/q) n + b` rises by as much as the linear terms do. The residues then decide
605+
it, as they decide a divisibility. `forall n in ZZ : floor(n/2) + ceil(n/2) = n` and Hermite's
606+
`floor(n/3) + floor((n + 1)/3) + floor((n + 2)/3) = n` are `True`, where they were left as written
607+
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).
608+
609+
| Input | Was (2.5.0) | Now |
610+
|---|---|---|
611+
| `forall n in ZZ : floor(n/2) + ceil(n/2) = n` | `UnhandledParseException` (left as written when quantifiers arrived) | `True` |
612+
| `exists n in ZZ : floor(n/2) + ceil(n/2) = n + 1` | `UnhandledParseException` (left as written when quantifiers arrived) | `False` |
613+
600614
### An antiderivative built through an even root says where the root is real
601615

602616
The linear-radical substitution, `u = (a x + b)^(1/q)`, and the general substitution for an even

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,10 @@ internal static bool IsIntegerSet(SpecialSet set)
863863
case Equalsf or Greaterf or GreaterOrEqualf or Lessf or LessOrEqualf:
864864
if (body is not IBinaryNode { NodeFirstChild: var lhs, NodeSecondChild: var rhs })
865865
return null;
866+
// Floors and ceilings of linear functions beside a linear part, where the
867+
// drifts cancel: floor(n/2) + ceil(n/2) - n repeats with 2.
868+
if (DriftlessPeriod(lhs - rhs, x) is { } rounding)
869+
return rounding;
866870
if (ResiduePeriod(lhs, x, set) is not { } l || ResiduePeriod(rhs, x, set) is not { } r)
867871
return null;
868872
return l * r / l.Gcd(r);
@@ -936,6 +940,50 @@ private static bool OnlyAbout(Entity condition, Variable x)
936940
}
937941
}
938942

943+
/// <summary>
944+
/// The period of a sum of floors and ceilings of linear functions of <paramref name="x"/>
945+
/// and of a linear part, where the drifts cancel, or <see langword="null"/>. A floor of
946+
/// <c>(p/q) x + b</c> rises by <c>p</c> when <c>x</c> rises by <c>q</c>, and a linear
947+
/// term by its slope times <c>q</c>, so where these sum to nothing the difference repeats
948+
/// with the least common multiple of the <c>q</c>: <c>floor(n/2) + ceil(n/2) - n</c> with
949+
/// 2, and Hermite's <c>floor(n/3) + floor((n + 1)/3) + floor((n + 2)/3) - n</c> with 3.
950+
/// https://github.com/asc-community/AngouriMath/issues/1409
951+
/// </summary>
952+
private static EInteger? DriftlessPeriod(Entity difference, Variable x)
953+
{
954+
var period = EInteger.One;
955+
var drift = ERational.Zero;
956+
var rounded = false;
957+
foreach (var term in Sumf.LinearChildren(difference))
958+
{
959+
if (!term.ContainsNode(x))
960+
continue;
961+
var (coefficient, core) = term is Mulf(Rational factor, var rest) ? (factor.ERational, rest) : (ERational.One, term);
962+
var argument = core switch
963+
{
964+
Floorf floor => floor.Argument,
965+
Ceilf ceiling => ceiling.Argument,
966+
_ => null,
967+
};
968+
if (Slope(argument ?? core, x) is not { } slope)
969+
return null;
970+
if (argument is not null)
971+
{
972+
rounded = true;
973+
var q = slope.Denominator.Abs();
974+
period = period * q / period.Gcd(q);
975+
}
976+
drift = drift.Add(coefficient.Multiply(slope));
977+
}
978+
return rounded && drift.IsZero ? period : null;
979+
}
980+
981+
/// <summary>The slope of an expression linear in <paramref name="x"/> with a rational one, or <see langword="null"/>.</summary>
982+
private static ERational? Slope(Entity expression, Variable x)
983+
=> TreeAnalyzer.TryGetPolyLinear(expression, x, out var a, out var b) && !b.ContainsNode(x) && a.Evaled is Rational rate
984+
? rate.ERational
985+
: null;
986+
939987
/// <summary>
940988
/// The period, in <paramref name="x"/>, of an expression's residue modulo
941989
/// <paramref name="modulus"/>: the modulus for a polynomial with whole coefficients,

‎Sources/Tests/UnitTests/Core/QuantifierTest.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ public void DecidedOverAFiniteSet(string statement, string expected)
9191
[InlineData("exists! k in ZZ : (0 < k and k < 7) and 3 divides k and 2 divides k", "True")]
9292
[InlineData("forall k in ZZ : (0 < k and k < 7 and 2 divides k) implies 3 divides binomial(7, k) - 1", "False")]
9393
[InlineData("forall n in ZZ : n >= 5 implies 2^n > n^2", "True")]
94+
// Floors and ceilings of n over a whole number beside n itself repeat where the drifts
95+
// cancel, and the residues decide them: floor(n/2) + ceil(n/2) = n, and Hermite's
96+
// identity for thirds.
97+
[InlineData("forall n in ZZ : floor(n/2) + ceil(n/2) = n", "True")]
98+
[InlineData("forall n in ZZ : floor(n/3) + floor((n + 1)/3) + floor((n + 2)/3) = n", "True")]
99+
[InlineData("forall n in ZZ : floor(n/2) = n/2", "False")]
100+
[InlineData("forall n in ZZ+ : ceil(n/2) - floor(n/2) <= 1", "True")]
101+
[InlineData("exists n in ZZ : floor(n/2) + ceil(n/2) = n + 1", "False")]
94102
// Prob 8.9.24 for every prime: that p is prime and 0 < k < p is established by the
95103
// quantifiers around the claim, in either order and with the hypothesis curried, and the
96104
// rule for p divides binomial(p, k) reads it. Without the primality, or without either

0 commit comments

Comments
 (0)