Jump to content

User:Chridd/sandbox1

From Wikipedia, the free encyclopedia

    # find primes from 3 up to max
    max = 50
    primes = []
    for n in range(3, max):
        composite = False
        for d in range(2, n-1):
            if n % d == 0:
                composite = True
                break
        if not composite:
            primes.append(n)
            
    count = len(primes)

    yes_marker = '[[Image:Yes check.svg|10px]]'   # tick (U.S. "check") for residues
    no_marker  = '[[Image:Black x.svg|10px]]'   # cross for non-residues

    def colortag(n):
        if n % 4 == 1:
            return 'bgcolor=#e0ffff'
        else:
            return 'bgcolor=#ffe0e0'

    # computes Legendre symbol (a/q)
    # assumes a and q positive, q prime, (a, q) = 1
    def legendre(a, q):
        for n in range(1, q-1):
            if (n * n) % q == a % q:
                return 1;
        return -1;
        
    # print table header
    print '{| class="wikitable"'
    print '|-'
    print '| || colspan=' + str(count+1), 'align="center" |', "''p''"
    print '|-'
    print '| rowspan=' + str(count+1), "|  ''q''  || ",
    for p in primes:
        print '||', colortag(p), 'align="center" style="border-bottom:2px solid" |', "'''" + str(p) + "'''", 
    print

    # now the main table
    for q in primes:
        # first column
        print '|-'
        print '|', colortag(q), 'align="right" style="border-right:2px solid" |', "''' " + str(q) + " '''",

        # remaining columns
        for p in primes:
            print '||', colortag(1+(p-1)*(q-1)/2), '|',

            if p == q:
                print ' ',
            else:
                # symbol for (p/q)
                if legendre(p, q) == 1:
                    print yes_marker,
                else:
                    print no_marker,

                if legendre(q, p) == 1:
                    print yes_marker,
                else:
                    print no_marker,
        print
            
    print '|}'

p
q 3 5 7 11 13 17 19 23 29 31 37 41 43 47
3
5
7
11
13
17
19
23
29
31
37
41
43
47