// Générer les sphères, tracer les balles et attester la parité
long Puissance3(int exposant)
{
long puissance = 1;
for (int k = 0; k < exposant; k++)
puissance = checked(puissance * 3);
return puissance;
}
long BallF2(int rayon) => checked(2 * Puissance3(rayon) - 1);
// Controle : recompter les spheres F_2 par generation effective (petits R)
int[] F2SpheresByGeneration(int rayonMax)
{
var effectifs = new int[rayonMax + 1];
var mots = new List<string> { "" };
effectifs[0] = mots.Count;
for (int r = 1; r <= rayonMax; r++)
{
var suivants = new List<string>();
foreach (string word in mots)
foreach (char c in ALPHA)
if (word.Length == 0 || c != INV[word[^1]])
suivants.Add(word + c);
mots = suivants;
effectifs[r] = mots.Count;
}
return effectifs;
}
// Références citées : effectifs affichés par Python pour les rayons 0 à 9.
const string sectionSpheres = "Sphères F₂";
var referenceSpheres = new[] { 1, 4, 12, 36, 108, 324, 972, 2_916, 8_748, 26_244 };
int[] spheresF2 = F2SpheresByGeneration(9);
Verifier(sectionSpheres, "nombre de rayons", spheresF2.Length, referenceSpheres.Length);
var lignesSpheres = new List<string>();
long cumulF2 = 0;
for (int r = 0; r < spheresF2.Length; r++)
{
string cas = FormattableString.Invariant($"r = {r}");
Verifier(sectionSpheres, $"{cas} : effectif Python", spheresF2[r], referenceSpheres[r]);
if (r > 0)
Verifier(sectionSpheres, $"{cas} : loi 4·3^(r−1)", (long)spheresF2[r], 4 * Puissance3(r - 1));
cumulF2 += spheresF2[r];
Verifier(sectionSpheres, $"{cas} : cumul des sphères contre balle", cumulF2, BallF2(r));
lignesSpheres.Add(FormattableString.Invariant($"{r}: '{spheresF2[r]:N0}'"));
}
Console.WriteLine("sphères F₂ générées (r : nombre) : {" + string.Join(", ", lignesSpheres) + "}");
Console.WriteLine("[PASS] sphères F₂ générées = 4·3^(r−1) pour r = 1..9");
// Figure : croissance (balle) de Z^2 (polynome) vs F_2 (exponentielle), echelle log
int[] rayonsFigure = Enumerable.Range(0, 16).ToArray();
int[] ballesZ2 = rayonsFigure.Select(BallZ2).ToArray();
long[] ballesF2 = rayonsFigure.Select(BallF2).ToArray();
// Points de référence issus des formules et du domaine du tracé Python (r = 0..15).
var referenceBallesZ2 = new[] { 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, 221, 265, 313, 365, 421, 481 };
var referenceBallesF2 = new long[] { 1, 5, 17, 53, 161, 485, 1_457, 4_373, 13_121, 39_365, 118_097, 354_293, 1_062_881, 3_188_645, 9_565_937, 28_697_813 };
const string sectionBalles = "Balles et figure";
Verifier(sectionBalles, "nombre de points Z²", ballesZ2.Length, referenceBallesZ2.Length);
Verifier(sectionBalles, "nombre de points F₂", ballesF2.Length, referenceBallesF2.Length);
for (int i = 0; i < rayonsFigure.Length; i++)
{
string cas = FormattableString.Invariant($"r = {i}");
Verifier(sectionBalles, $"{cas} : abscisse", rayonsFigure[i], i);
Verifier(sectionBalles, $"{cas} : balle Z²", ballesZ2[i], referenceBallesZ2[i]);
Verifier(sectionBalles, $"{cas} : balle F₂", ballesF2[i], referenceBallesF2[i]);
}
// Références citées : valeurs et ratio imprimés sous la figure Python.
int rayonFinal = rayonsFigure[^1];
int finaleZ2 = ballesZ2[^1];
long finaleF2 = ballesF2[^1];
string ratioFinal = ((double)finaleF2 / finaleZ2).ToString("0.00e+00", invariant);
Verifier(sectionBalles, "rayon final affiché", rayonFinal, 15);
Verifier(sectionBalles, "balle Z² finale affichée", finaleZ2, 481);
Verifier(sectionBalles, "balle F₂ finale affichée", finaleF2, 28_697_813L);
Verifier(sectionBalles, "ratio final affiché", ratioFinal, "5.97e+04");
// Le noyau reçoit le SVG lui-même, et non une chaîne échappée en HTML ou en texte.
class Graphique
{
public Graphique(string svg) => Svg = svg;
public string Svg { get; }
}
Formatter.Register<Graphique>((graphique, writer) => writer.Write(graphique.Svg), "image/svg+xml");
// Abscisse linéaire ; les graduations verticales sont espacées d'une décennie.
const int gauche = 100, droite = 770, haut = 62, bas = 420;
double XFigure(int rayon) => gauche + (droite - gauche) * rayon / (double)rayonFinal;
double YFigure(double balle) => bas - (bas - haut) * Math.Log10(balle) / 8.0;
var svgCroissance = new StringBuilder();
void LigneSvg(FormattableString ligne) => svgCroissance.AppendLine(ligne.ToString(invariant));
svgCroissance.AppendLine("<svg xmlns='http://www.w3.org/2000/svg' width='800' height='500' viewBox='0 0 800 500' role='img' aria-label='Croissance des balles de Z² et F₂ en échelle logarithmique'>");
svgCroissance.AppendLine("<rect width='800' height='500' fill='white'/><g font-family='sans-serif' font-size='13' fill='#222'>");
svgCroissance.AppendLine("<text x='400' y='31' text-anchor='middle' font-size='17'>Croissance du graphe de Cayley — Z² polynomial vs F₂ exponentiel</text>");
for (int decade = 0; decade <= 8; decade++)
{
double y = YFigure(Math.Pow(10, decade));
LigneSvg($"<line x1='{gauche}' y1='{y:0.###}' x2='{droite}' y2='{y:0.###}' stroke='#ddd'/>");
LigneSvg($"<text x='{gauche - 9}' y='{y + 4:0.###}' text-anchor='end'>10<tspan baseline-shift='super' font-size='9'>{decade}</tspan></text>");
}
foreach (int rayon in rayonsFigure)
{
double x = XFigure(rayon);
LigneSvg($"<line x1='{x:0.###}' y1='{haut}' x2='{x:0.###}' y2='{bas}' stroke='#eee'/>");
LigneSvg($"<text x='{x:0.###}' y='{bas + 20}' text-anchor='middle'>{rayon}</text>");
}
LigneSvg($"<path d='M {gauche} {haut} V {bas} H {droite}' fill='none' stroke='#333'/>");
svgCroissance.AppendLine("<text x='435' y='475' text-anchor='middle'>rayon R</text><text x='29' y='241' transform='rotate(-90 29 241)' text-anchor='middle'>éléments à distance ≤ R (échelle log)</text>");
void TracerSerie(IEnumerable<long> valeurs, string couleur, bool carres)
{
var points = valeurs.Select((valeur, i) => (X: XFigure(rayonsFigure[i]), Y: YFigure(valeur))).ToArray();
string coordonnees = string.Join(" ", points.Select(p => FormattableString.Invariant($"{p.X:0.###},{p.Y:0.###}")));
LigneSvg($"<polyline points='{coordonnees}' fill='none' stroke='{couleur}' stroke-width='2'/>");
foreach (var p in points)
{
if (carres)
LigneSvg($"<rect x='{p.X - 3:0.###}' y='{p.Y - 3:0.###}' width='6' height='6' fill='{couleur}'/>");
else
LigneSvg($"<circle cx='{p.X:0.###}' cy='{p.Y:0.###}' r='3' fill='{couleur}'/>");
}
}
TracerSerie(ballesZ2.Select(b => (long)b), "#1f77b4", false);
TracerSerie(ballesF2, "#d62728", true);
svgCroissance.AppendLine("<rect x='112' y='71' width='284' height='55' rx='4' fill='white' stroke='#ddd'/>");
svgCroissance.AppendLine("<path d='M 123 88 H 149' stroke='#1f77b4' stroke-width='2'/><circle cx='136' cy='88' r='3' fill='#1f77b4'/><text x='157' y='92'>Z² (balle = 2R²+2R+1, polynôme)</text>");
svgCroissance.AppendLine("<path d='M 123 110 H 149' stroke='#d62728' stroke-width='2'/><rect x='133' y='107' width='6' height='6' fill='#d62728'/><text x='157' y='114'>F₂ (balle = 2·3^R−1, exponentiel)</text></g></svg>");
display(new Graphique(svgCroissance.ToString()), "image/svg+xml");
Console.WriteLine(FormattableString.Invariant($"balle Z²({rayonFinal}) = {finaleZ2:N0} · balle F₂({rayonFinal}) = {finaleF2:N0} (ratio {ratioFinal}×)"));
// Le bilan exige des comparaisons exécutées dans chacune des sections du carnet.
var sectionsParite = new[] { "Graine", "Formes normales", "Problème du mot", sectionAutomateZ2, sectionAutomateF2, "Accord automate/oracle", "BFS / automate", sectionSpheres, sectionBalles };
Console.WriteLine("\nAttestation de parité — écarts par section");
foreach (string section in sectionsParite)
{
if (!parite.TryGetValue(section, out var bilan) || bilan.Comparaisons <= 0 || bilan.Ecarts != 0)
throw new InvalidOperationException($"Bilan de parité invalide : {section}.");
Console.WriteLine(FormattableString.Invariant($"CONFORME — {section} : {bilan.Ecarts} écart(s) sur {bilan.Comparaisons} comparaison(s)"));
}