Back to Home

Grouping natural numbers into n/2 pairwise coprime groups

The article describes the algorithm for dividing natural numbers 1..n into n/2 groups with pairwise coprime elements inside. Uses formula for odd composites and properties of 4k±1. Python code and justification provided.

How to group numbers 1..n into pairwise coprime pairs
Advertisement 728x90

Grouping Natural Numbers into Pairs of Coprime Numbers

Splitting the sequence {1, 2, 3, ..., n} into n/2 groups, where all numbers within each group are coprime, is achieved using a simple algorithm. Prime numbers and 1 are placed in the first group. Even numbers >2 form the starting elements of the remaining groups. Odd composite numbers are distributed using the formula pos_{x_i} = ⌊x_i/4 + 0.5⌋. This ensures the gcd of each odd number with the even group leader equals 1.

Distribution Logic

The algorithm relies on the properties of odd composite numbers: they all take the form 4k ± 1 for some k. Here, k corresponds to the group number. For the even group leader 2k, gcd(2k, 4k ± 1) = 1, since 4k ± 1 ≡ ±1 (mod 2k).

The first group contains:

Google AdInline article slot
  • 1
  • All prime numbers

Remaining groups (k ≥ 2):

  • First position: even number 2k
  • Second position (optional): odd composite numbers x, where pos_x = k

Some groups may contain 0 or 2 odd numbers (examples: k=3,7 — empty; k=14,16 — two each).

Example for n=24

A table demonstrates the distribution:

Google AdInline article slot

| Group 1 | 1, 2, 3, 5, 7, 11, 13, 17, 19, 23 |

| Group 2 | 4, 9, 15, 21 |

| Group 3 | 6 |

Google AdInline article slot

| Group 4 | 8, 25? (for n=24: 8) |

| ... | ... |

Verification of coprimality within groups is confirmed by properties: prime numbers are pairwise coprime, gcd(2k, 4k±1)=1.

Mathematical Justification

  • Prime numbers: Any two primes p, q (p ≠ q) have gcd(p,q)=1.
  • Even leaders: 2k with k≥2 is odd, but 2k is even.
  • Odd composites: x = 4k ±1, gcd(2k, x) = gcd(2k, ±1 mod 2k) =1.
  • Between odds in a group: Possible pairs (rare) are also coprime by construction, as they are distributed modulo.

This follows indirectly from Dirichlet's theorem on primes in arithmetic progressions, but directly from the form of odd numbers.

Python Implementation

For automating the splitting:

def group_naturals(n):
    primes = [1]  # 1 + primes
    composites_odd = []
    evens = []
    
    for i in range(2, n+1):
        if i % 2 == 0:
            evens.append(i)
        elif is_prime(i):
            primes.append(i)
        else:
            composites_odd.append(i)
    
    groups = [primes]
    pos = {}
    for x in composites_odd:
        k = int(x / 4 + 0.5)
        if k not in pos:
            pos[k] = []
        pos[k].append(x)
    
    for k in range(2, n//2 +1):
        group = [2*k]
        if k in pos:
            group.extend(sorted(pos[k]))
        groups.append(group)
    
    return groups[:n//2]  # trim extras

def is_prime(num):
    if num < 2:
        return False
    for i in range(2, int(num**0.5)+1):
        if num % i == 0:
            return False
    return True

The code generates groups for a given n, verifying properties.

Key Points

  • All groups contain only coprime numbers.
  • The number of groups is exactly n/2 for any natural n.
  • Odd composites are distributed by pos = ⌊x/4 + 0.5⌋.
  • Justification: x=4k±1 ⇒ gcd(2k, x)=1.
  • First group: 1 and all prime numbers.

— Editorial Team

Advertisement 728x90

Read Next