From 8c6460d2c3179e416cbc537d6e9e6e0a6ac209d7 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 28 Sep 2026 18:21:39 +0000 Subject: [PATCH] The least member of a set is found by search where its condition is not a bound min(x, x in S and P) was decided only where P is a bound. Where it is not, the members of ZZ+, ZZ* or PP are tried in order from the least, and the first whose condition evaluates to True is the least: Sullivan and Mackey's Prob 8.9.3, the least n with n binomial(n - 1, 2) >= 14, is 5. A condition that does not evaluate to a truth value, or none of the first 4096 members holding, leaves it as written. Part of #1409. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 6 +++- .../Functions/Continuous/ExtremumOverSet.cs | 36 ++++++++++++++++++- .../Tests/UnitTests/Core/Sets/PrimeSetTest.cs | 6 ++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index edd19a65c..66ba7e557 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -906,7 +906,10 @@ chain. `min(S)` with one argument that is a set is the least member of the set, `min(PP)` was `PP` itself — a one-argument `min` was the argument — and `min(x, x in S and P)` is the least member of `{ x in S : P }`, where it was left as written; both decided for `PP`, `ZZ+`, `ZZ*` and their cuts by an interval or a bound, so `min(x, x in PP and x > 14)` is the next -prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)). +prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)); and for a condition +that is not a bound by trying the members in order from the least, so Sullivan and Mackey's Prob +8.9.3, the least `n` with `n binomial(n - 1, 2) >= 14`, is `5` +([#1409](https://github.com/asc-community/AngouriMath/issues/1409)). | Input | Was (2.5.0) | Now | |---|---|---| @@ -918,6 +921,7 @@ prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)). | `"PP intersect [1; 30]".ToEntity().Evaled` | as written | `{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }` | | `"min(ZZ+)".ToEntity().Evaled` | `ZZ+` | `1` | | `"max(x, x in PP and x < 14)".ToEntity().Evaled` | as written | `13` | +| `"min(x, x in ZZ+ and x^2 > 50)".ToEntity().Evaled` | `UnhandledParseException` — `ZZ+` is new since | `8` | | `"prime(25)".ToEntity().Evaled` | `prime * 25` — juxtaposition of a variable `prime` | `97`; `prime(n)` is the `n`-th prime, `NaN` off the positive whole numbers | | `"valuation(12, 2)".ToEntity().Evaled` | `UnrecognizedFunctionParseException` | `2`; `valuation(n, p)` is the `p`-adic valuation, `+oo` at `0` and `NaN` off the primes | diff --git a/Sources/AngouriMath/Functions/Continuous/ExtremumOverSet.cs b/Sources/AngouriMath/Functions/Continuous/ExtremumOverSet.cs index 1d3fab1db..aef063248 100644 --- a/Sources/AngouriMath/Functions/Continuous/ExtremumOverSet.cs +++ b/Sources/AngouriMath/Functions/Continuous/ExtremumOverSet.cs @@ -162,12 +162,46 @@ private static (Entity value, List points)? Find(Entity expression, Enti return Integer.Create(first); } case ConditionalSet { DeclaredMembership: var (declared, rest), Var: Variable y } when declared.InnerSimplified is Set over: - return BoundsAsInterval(rest, y) is { } bounded ? LeastWholeMember(new Intersectionf(over, bounded)) : null; + return BoundsAsInterval(rest, y) is { } bounded ? LeastWholeMember(new Intersectionf(over, bounded)) : FirstThatHolds(over, y, rest); default: return null; } } + /// + /// The least member of the whole numbers at which + /// evaluates to True, found by trying them in order from + /// the least up: every member before it was tried and evaluated to False, so it is the + /// least. Sullivan and Mackey's Prob 8.9.3, + /// min(n, n in { n in ZZ+ : n binomial(n - 1, 2) >= 14 }), is 5. + /// where a member's condition does not evaluate to a truth value, or none of the first + /// holds. https://github.com/asc-community/AngouriMath/issues/1409 + /// + private static Integer? FirstThatHolds(Set over, Variable y, Entity condition) + { + if (LeastWholeMember(over) is not { } least || over is not SpecialSet special) + return null; + var prime = special.ToDomain() == Core.Domain.Prime; + EInteger? member = least.EInteger; + for (var tried = 0; tried < MaxMembersSearched && member is { } current; tried++) + { + switch (condition.Substitute(y, Integer.Create(current)).Evaled) + { + case Entity.Boolean(true): + return Integer.Create(current); + case Entity.Boolean(false): + member = prime ? Primes.NextPrime(current + 1) : current + 1; + continue; + default: + return null; + } + } + return null; + } + + /// How many members tries before declining. + private const int MaxMembersSearched = 4096; + /// /// A conjunction of bounds on -- y > a, y >= a, /// a < y, y < b and their kin -- as the interval they cut, or diff --git a/Sources/Tests/UnitTests/Core/Sets/PrimeSetTest.cs b/Sources/Tests/UnitTests/Core/Sets/PrimeSetTest.cs index b4debf3a0..c6dec42c1 100644 --- a/Sources/Tests/UnitTests/Core/Sets/PrimeSetTest.cs +++ b/Sources/Tests/UnitTests/Core/Sets/PrimeSetTest.cs @@ -67,6 +67,11 @@ public sealed class PrimeSetTest [InlineData("min(x, x in ZZ+ and x >= 7/2)", "4")] [InlineData("min(x, x in PP and x > 14 and x < 20)", "17")] [InlineData("min(x, 3 < x and x in ZZ+ and x <= 5)", "4")] + // A condition that is not a bound, decided member by member from the least up: Sullivan + // and Mackey's Prob 8.9.3, the least n with n binomial(n - 1, 2) >= 14. + [InlineData("min(n, n in { n in ZZ+ : n * binomial(n - 1, 2) >= 14 })", "5")] + [InlineData("min(x, x in ZZ+ and x^2 > 50)", "8")] + [InlineData("min(p, p in PP and p^2 > 200)", "17")] // The greatest member, and the previous prime before a bound. [InlineData("max(x, x in PP and x < 14)", "13")] [InlineData("max(x, x in PP and x <= 2)", "2")] @@ -113,6 +118,7 @@ public void Decided(string input, string expected) [InlineData("max(x, x in PP and x < 2)", typeof(Maximumf))] [InlineData("max(x, x in ZZ+ and x < 1)", typeof(Maximumf))] [InlineData("min(x, x in PP and x > 14 and x < 16)", typeof(Minimumf))] + [InlineData("min(x, x in ZZ+ and x^2 < 0)", typeof(Minimumf))] [InlineData("PP intersect [1; 10^7]", typeof(Set.Intersectionf))] [InlineData("prime(n)", typeof(Primef))] [InlineData("prime(10^9)", typeof(Primef))]