/*
  Copyright (c) 2021 Corinne Diakhoumpa et Erwan Iev-Le Tac

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

const assert = require('assert').strict;

// This program enumerates the subgroups of (Z/${N}Z)* for N=229 using brute
// force search which gives the subfields of the cyclotomic field ℚ(ζn) by
// the Galois correspondance. At the end, only the prime factorization of
// N - 1 = 2 * 2 * 3 * 19 was necessary for the diagram of "Cyclotomique" but
// it was still helpful to check consistency of the result.

let N = 229;
function canonicalRepresentant(x) {
    let c = x % N;
    assert.notStrictEqual(c, 0);
    if (c > N / 2) c -= N;
    return c;
}

let orders = {};
for (let i = 1; i < N; i++) {
    let v = canonicalRepresentant(i);
    let s = [];
    let order;
    for (order = 1; ; order++) {
        s.push(v);
        if (v == 1)
            break;
        v = canonicalRepresentant((v + N) * i);
    }
    s = s.sort((a, b) => { return a - b; });
    if (!orders[order])
        orders[order] = [];
    orders[order].push(canonicalRepresentant(i));
}

console.log(`Subgroups of (Z/${N}Z)*:`)
for (let item in orders) {
    console.log(`Order ${item}: ${orders[item].sort((a, b) => { return Math.abs(a) - Math.abs(b); })}\n`);
}