Graphs and Square Roots of Modulo P
John Cook looks at the advanced mathematics concept of modulo p, and creates charts to explore them in Python.
Join the DZone community and get the full member experience.Join For Free
imagine a clock with a prime number of hours. so instead of being divided into 12 hours, it might be divided into 11 or 13, for example. you can add numbers on this clock the way you’d add numbers on an ordinary clock: add the two numbers as ordinary integers and take the remainder by p , the number of hours on the clock. you can multiply them the same way: multiply as ordinary integers, then take the remainder by p . this is called arithmetic modulo p , or just mod p .
which numbers have square roots mod p ? that is, for which numbers x does there exist a number y such that y 2 = x mod p ? it turns out that of the non-zero numbers, half have a square root and half don’t. the numbers that have square roots are called quadratic residues and the ones that do not have square roots are called quadratic nonresidues . zero is usually left out of the conversation. for example, if you square the numbers 1 through 6 and take the remainder mod 7, you get 1, 4, 2, 2, 4, and 1. so mod 7 the quadratic residues are 1, 2, and 4, and the nonresidues are 3, 5, and 6.
a simple, brute force way to tell whether a number is a quadratic residue mod p is to square all the numbers from 1 to p -1 and see if any of the results equals your number. there are more efficient algorithms, but this is the easiest to understand.
at first it seems that the quadratic residues and nonresidues are scattered randomly. for example, here are the quadratic residues mod 23:
[1, 2, 3, 4, 6, 8, 9, 12, 13, 16, 18]
but there are patterns, such as the famous quadratic reciprocity theorem. we can see some pattern in the residues by visualizing them on a graph. we make the numbers mod p vertices of a graph, and join two vertices a and b with an edge if a – b is a quadratic residue mod p .
here’s the resulting graph mod 13:
and here’s the corresponding graph for p = 17:
and here’s python code to produce these graphs:
import networkx as nx import matplotlib.pyplot as plt def residue(x, p): for i in range(1, p): if (i*i - x) % p == 0: return true return false p = 17 g = nx.graph() for i in range(p): for j in range(i+1, p): if residue(i-j, p): g.add_edge(i, j) nx.draw_circular(g) plt.show()
however, this isn’t the appropriate graph for all values of p . the graphs above are undirected graphs. we’ve implicitly assumed that if there’s an edge between a and b then there should be an edge between b and a . and that’s true for p = 13 or 17, but not, for example, for p = 11. why’s that?
if p leaves a remainder of 1 when divided by 4, i.e. p = 1 mod 4, then x is a quadratic residue mod p if and only if – x is a quadratic residue mod p . so if a – b is a residue, so is b – a . this means an undirected graph is appropriate. but if p = 3 mod 4, x is a residue mod p if and only if – x is a non-residue mod p . this means we should use directed graphs if p = 3 mod 4. here’s an example for p = 7.
the code for creating the directed graphs is a little different:
g = nx.digraph() for i in range(p): for j in range(p): if residue(i-j, p): g.add_edge(i, j)
unfortunately, the way networkx draws directed graphs is disappointing. a directed edge from a to b is drawn with a heavy line segment on the b end. any suggestions for a python package that draws more attractive directed graphs?
the idea of creating graphs this way came from roger cook’s chapter on number theory applications in graph connections . (i’m not related to roger cook as far as i know.)
you could look at quadratic residues modulo composite numbers too. and i imagine you could also make some interesting graphs using legendre symbols.
Published at DZone with permission of John Cook, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.