@@ -310,10 +310,14 @@ private static Entity Quantified(Kind kind, Entity var, Entity over, Entity body
310310 // the residue of infinitely many primes (Dirichlet), so a statement that repeats modulo
311311 // M is decided over the primes by M's own prime factors, one by one, and by the units
312312 // modulo M: p^2 - 1 is a multiple of 24 for every prime past 3, though not for 3 --
313- // Sullivan and Mackey's Prob 6.7.5. https://github.com/asc-community/AngouriMath/issues/1409
314- if ( set is SpecialSet . Primes && kind != Kind . Unique && OverThePrimes ( kind , x , body , isExact ) is { } overThePrimes )
315- {
316- by = ( "the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes" , "Nat.setOf_prime_and_eq_mod_infinite" ) ;
313+ // Sullivan and Mackey's Prob 6.7.5. A comparison keeps one truth value past its root,
314+ // so the primes below the last root are asked one by one: every prime is 2, or 1 or 3
315+ // modulo 4. https://github.com/asc-community/AngouriMath/issues/1409
316+ if ( set is SpecialSet . Primes && kind != Kind . Unique && OverThePrimes ( kind , x , body , isExact ) is var ( overThePrimes , threshold ) )
317+ {
318+ by = ( threshold . CompareTo ( EInteger . FromInt32 ( 2 ) ) > 0
319+ ? $ "the primes below { threshold } one by one; from { threshold } the comparisons have settled, the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes"
320+ : "the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes" , "Nat.setOf_prime_and_eq_mod_infinite" ) ;
317321 return overThePrimes ;
318322 }
319323 if ( set is FiniteSet finite )
@@ -969,22 +973,44 @@ private static (EInteger Period, EInteger Threshold)? Settled(Entity body, Varia
969973 any = true ;
970974 continue ;
971975 }
972- if ( LinearComparison ( atom , x ) is not ( var slope , var rest ) || rest . Evaled is not Real { IsNaN : false } intercept
973- || ! slope . IsFinite )
976+ if ( SettlesAt ( atom , x ) is not { } settlesAt )
974977 return null ;
975978 any = true ;
976- // Flat, or against an infinity -- x < +oo -- it has one truth value throughout.
977- if ( slope . IsZero || ! intercept . IsFinite )
978- continue ;
979- // Past floor(-b / a) + 1 the sign of a x + b no longer changes.
980- var root = intercept . EDecimal . Negate ( ) . Divide ( slope . EDecimal , PeterO . Numbers . EContext . ForPrecision ( 64 ) ) ;
981- var settlesAt = root . RoundToExponent ( 0 , PeterO . Numbers . ERounding . Floor ) . ToEInteger ( ) . Add ( EInteger . One ) ;
982979 if ( settlesAt . CompareTo ( threshold ) > 0 )
983980 threshold = settlesAt ;
984981 }
985982 return any ? ( period , threshold ) : null ;
986983 }
987984
985+ /// <summary>
986+ /// The whole number from which a comparison linear in <paramref name="x"/>, with numbers
987+ /// for coefficients, keeps one truth value, or <see langword="null"/> for an atom that is
988+ /// not one: past its root the sign of <c>a x + b</c> no longer changes, so from
989+ /// <c>floor(-b / a) + 1</c> on. Zero for a comparison that never changes.
990+ /// </summary>
991+ private static EInteger ? SettlesAt ( Entity atom , Variable x )
992+ {
993+ if ( LinearComparison ( atom , x ) is not ( var slope , var rest ) || rest . Evaled is not Real { IsNaN : false } intercept
994+ || ! slope . IsFinite )
995+ return null ;
996+ // Flat, or against an infinity -- x < +oo -- it has one truth value throughout.
997+ if ( slope . IsZero || ! intercept . IsFinite )
998+ return EInteger . Zero ;
999+ // Exactly where both are rational, so that where it settles does not depend on the
1000+ // decimal precision: at 16 or 30 digits 2/3 rounds up, which put the root of 2/3 n - 2
1001+ // below 3, and 3 was taken for a member past it.
1002+ if ( slope is Rational { ERational : var a } && intercept is Rational { ERational : var b } )
1003+ {
1004+ var root = b . Negate ( ) . Divide ( a ) ;
1005+ var floor = root . ToEInteger ( ) ;
1006+ if ( root . CompareTo ( ERational . FromEInteger ( floor ) ) < 0 )
1007+ floor -= EInteger . One ;
1008+ return floor . Add ( EInteger . One ) ;
1009+ }
1010+ var approximated = intercept . EDecimal . Negate ( ) . Divide ( slope . EDecimal , EContext . ForPrecision ( 64 ) ) ;
1011+ return approximated . RoundToExponent ( 0 , ERounding . Floor ) . ToEInteger ( ) . Add ( EInteger . One ) ;
1012+ }
1013+
9881014 /// <summary>
9891015 /// The statement decided at every member from <paramref name="first"/> up to, and not
9901016 /// including, <paramref name="past"/>.
@@ -1069,36 +1095,49 @@ private static Entity OneOfTheFirst(Variable x, EInteger from, int count, Entity
10691095 return any ?? Entity . Boolean . False ;
10701096 }
10711097
1072-
10731098 /// <summary>
1074- /// The period of the body in <paramref name="x"/> over the whole numbers, where it has
1075- /// one: a divisibility by a whole number, or a congruence modulo one, of polynomials
1076- /// with whole coefficients, joined by the connectives; the least common multiple of the
1077- /// parts' periods.
1099+ /// The statement over the primes, where past a threshold it repeats modulo some <c>M</c>,
1100+ /// and the threshold, or <see langword="null"/> where what is left does not repeat or a
1101+ /// member does not decide it. A comparison linear in <paramref name="x"/> keeps one truth
1102+ /// value past its root, so the primes below the last root are asked one by one. Past it
1103+ /// the statement is its value at each prime factor of <c>M</c> there, and at each unit
1104+ /// modulo <c>M</c>, which stands for the infinitely many primes of its class there.
10781105 /// </summary>
1079- /// <summary>
1080- /// The statement over the primes, where past a lower bound on them it repeats modulo some
1081- /// <c>M</c>: at each prime factor of <c>M</c> past the bound, and at each unit modulo
1082- /// <c>M</c>, which stands for the infinitely many primes of its class. <see langword="null"/>
1083- /// where the statement does not repeat, or a residue does not decide it.
1084- /// </summary>
1085- private static Entity ? OverThePrimes ( Kind kind , Variable x , Entity body , bool isExact )
1106+ private static ( Entity Verdict , EInteger Threshold ) ? OverThePrimes ( Kind kind , Variable x , Entity body , bool isExact )
10861107 {
1087- // The primes asked about: those past a bound the hypothesis sets, or all of them.
1088- var above = EInteger . One ;
1089- var claim = body ;
1090- if ( kind == Kind . All && body is Impliesf ( var assumption , var conclusion ) && LowerBound ( assumption , x ) is { } bound )
1091- ( above , claim ) = ( bound , conclusion ) ;
1092- else if ( kind == Kind . Some && body is Andf ( var first , var second ) )
1093- {
1094- if ( LowerBound ( first , x ) is { } firstBound )
1095- ( above , claim ) = ( firstBound , second ) ;
1096- else if ( LowerBound ( second , x ) is { } secondBound )
1097- ( above , claim ) = ( secondBound , first ) ;
1098- }
1099- if ( ! claim . ContainsNode ( x ) || Period ( claim , x , ( SpecialSet ) MathS . Sets . Z ) is not { } modulus
1100- || modulus . CompareTo ( EInteger . FromInt32 ( LargestPeriod ) ) > 0 )
1108+ // The comparisons, and the least number from which every one of them has settled .
1109+ var atoms = new List < Entity > ( ) ;
1110+ CollectAtoms ( body , atoms ) ;
1111+ var two = EInteger . FromInt32 ( 2 ) ;
1112+ var threshold = two ;
1113+ var comparisons = new List < Entity > ( ) ;
1114+ foreach ( var atom in atoms )
1115+ if ( atom . ContainsNode ( x ) && SettlesAt ( atom , x ) is { } settlesAt )
1116+ {
1117+ comparisons . Add ( atom ) ;
1118+ if ( settlesAt . CompareTo ( threshold ) > 0 )
1119+ threshold = settlesAt ;
1120+ }
1121+ if ( threshold . CompareTo ( EInteger . FromInt32 ( LargestPrefix ) ) > 0 )
11011122 return null ;
1123+ // From the threshold on, each comparison is the truth value it has there.
1124+ var settled = new Dictionary < Entity , Entity > ( ) ;
1125+ foreach ( var comparison in comparisons )
1126+ {
1127+ if ( comparison . Substitute ( x , Integer . Create ( threshold ) ) . Evaled is not Entity . Boolean value )
1128+ return null ;
1129+ settled [ comparison ] = value ;
1130+ }
1131+ var past = settled . Count == 0 ? body
1132+ : body . Replace ( node => settled . TryGetValue ( node , out var value ) ? value : node ) . InnerSimplified ( isExact ) ;
1133+ // What is left repeats, or is one truth value, which repeats modulo 1.
1134+ var modulus = EInteger . One ;
1135+ if ( past . ContainsNode ( x ) )
1136+ {
1137+ if ( Period ( past , x , ( SpecialSet ) MathS . Sets . Z ) is not { } period || period . CompareTo ( EInteger . FromInt32 ( LargestPeriod ) ) > 0 )
1138+ return null ;
1139+ modulus = period ;
1140+ }
11021141 int holds = 0 , fails = 0 , undecided = 0 ;
11031142 void Tell ( Entity verdict )
11041143 {
@@ -1109,9 +1148,13 @@ void Tell(Entity verdict)
11091148 default : undecided ++ ; break ;
11101149 }
11111150 }
1112- // The prime factors of M past the bound, which are not units modulo it.
1151+ // The primes below the threshold, one by one.
1152+ for ( var prime = two ; prime . CompareTo ( threshold ) < 0 ; prime += 1 )
1153+ if ( Functions . Primes . IsPrime ( prime ) == true )
1154+ Tell ( body . Substitute ( x , Integer . Create ( prime ) ) . InnerSimplified ( isExact ) ) ;
1155+ // The prime factors of M from the threshold on, which are not units modulo it.
11131156 var rest = modulus ;
1114- for ( var factor = EInteger . FromInt32 ( 2 ) ; rest . CompareTo ( EInteger . One ) > 0 ; factor += 1 )
1157+ for ( var factor = two ; rest . CompareTo ( EInteger . One ) > 0 ; factor += 1 )
11151158 {
11161159 // Past the square root of what is left, what is left is prime.
11171160 if ( factor . Multiply ( factor ) . CompareTo ( rest ) > 0 )
@@ -1120,31 +1163,24 @@ void Tell(Entity verdict)
11201163 continue ;
11211164 while ( rest . Remainder ( factor ) . IsZero )
11221165 rest = rest . Divide ( factor ) ;
1123- if ( factor . CompareTo ( above ) > 0 )
1124- Tell ( claim . Substitute ( x , Integer . Create ( factor ) ) . InnerSimplified ( isExact ) ) ;
1166+ if ( factor . CompareTo ( threshold ) >= 0 )
1167+ Tell ( past . Substitute ( x , Integer . Create ( factor ) ) . InnerSimplified ( isExact ) ) ;
11251168 }
1126- // The units modulo M, each the residue of primes past any bound .
1169+ // The units modulo M, each the residue of infinitely many primes past the threshold .
11271170 for ( var residue = EInteger . One ; residue . CompareTo ( modulus ) <= 0 ; residue += 1 )
11281171 if ( residue . Gcd ( modulus ) . Equals ( EInteger . One ) )
1129- Tell ( claim . Substitute ( x , Integer . Create ( residue ) ) . InnerSimplified ( isExact ) ) ;
1130- return Tally ( kind , holds , fails , undecided , distinct : true ) ;
1172+ Tell ( past . Substitute ( x , Integer . Create ( residue ) ) . InnerSimplified ( isExact ) ) ;
1173+ if ( Tally ( kind , holds , fails , undecided , distinct : true ) is not { } verdict )
1174+ return null ;
1175+ return ( verdict , threshold ) ;
11311176 }
11321177
11331178 /// <summary>
1134- /// The whole number every <paramref name="x"/> satisfying <paramref name="condition"/> is
1135- /// past, where the condition is a lower bound on it: <c>x > 3</c> is <c>3</c>,
1136- /// <c>x >= 5</c> is <c>4</c>. <see langword="null"/> otherwise.
1179+ /// The period of the body in <paramref name="x"/> over the whole numbers, where it has
1180+ /// one: a divisibility by a whole number, or a congruence modulo one, of polynomials
1181+ /// with whole coefficients, joined by the connectives; the least common multiple of the
1182+ /// parts' periods.
11371183 /// </summary>
1138- private static EInteger ? LowerBound ( Entity condition , Variable x )
1139- => condition switch
1140- {
1141- Greaterf ( var name , Integer { EInteger : var past } ) when name == x => past ,
1142- GreaterOrEqualf ( var name , Integer { EInteger : var from } ) when name == x => from . Subtract ( EInteger . One ) ,
1143- Lessf ( Integer { EInteger : var past } , var name ) when name == x => past ,
1144- LessOrEqualf ( Integer { EInteger : var from } , var name ) when name == x => from . Subtract ( EInteger . One ) ,
1145- _ => null ,
1146- } ;
1147-
11481184 private static EInteger ? Period ( Entity body , Variable x , SpecialSet set )
11491185 {
11501186 switch ( body )
0 commit comments