-
Notifications
You must be signed in to change notification settings - Fork 3
/
4ni4n.py
102 lines (90 loc) · 2.36 KB
/
4ni4n.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
from sympy import *
from collections import Counter
# explore primes and sums of squares and the pattern 4n+1 , 4n+3
#
# this uses 的 for sum of 2 squares and 質 for prime. These are chinese symbols.
# The symbols are parts of related phrases in Chinese.
squares = Counter()
squares[0]=0
squares[1]=1
squares[4]=2
squares[9]=3
def coprime(a,b):
pa,pb=primefactors(a),primefactors(b)
pc=set(pa).intersection(set(pb))
if pc==set([]): return true
return false
def extend_squares(n):
global squares
while n > max(squares.keys()):
maxroot = squares[max(squares.keys())]
squares[ (maxroot+1)**2 ] = maxroot+1
#print squares
# given integer n, find the two squares that sum to it, or None,None if none do
def square_addends(n):
if not n in squares.keys():
extend_squares(n)
for i in squares.keys():
for j in squares.keys():
if i+j==n:
return i,j
return None,None
# is the number n the sum of two square integers?
def issquaresum(n):
if square_addends(n) == (None,None): return false
return true
# latex pretty print factors of n
def ppfactorint(n):
s=str(factorint(n))
s=s.replace(': ','^')
s=s.replace('}',',')
#s=s.replace('}',')')
s=s.replace('{','')
s=s.replace(', ',' ')
s=s.replace('^1 ',' ')
s=s.replace('^1,',',')
s=s.replace(' ','*')
return s
tr = ' '
td = ''
for i in range(0,20):
print td+'%0.3i' % (4*i+1),
print td+'%0.3i' % (4*i+3),
if isprime(4*i+1): print td+'質',
else: print td+'--',
if isprime(4*i+3): print td+'質',
else: print td+'--',
print square_addends(4*i+1),
print square_addends(4*i+3),
print factorint(4*i+1),
print factorint(4*i+3),
print
print
for i in range(0,20):
print tr,
print td+'%0.2i' % (4*i+0),
print td+'%0.2i' % (4*i+1),
print td+'%0.2i' % (4*i+2),
print td+'%0.2i' % (4*i+3),
if isprime(4*i+0): print td+'質',
else: print td+'--',
if isprime(4*i+1): print td+'質',
else: print td+'--',
if isprime(4*i+2): print td+'質',
else: print td+'--',
if isprime(4*i+3): print td+'質',
else: print td+'--',
if issquaresum(4*i+0): print td+'总',
else: print td+'--',
if issquaresum(4*i+1): print td+'总',
else: print td+'--',
if issquaresum(4*i+2): print td+'总',
else: print td+'--',
if issquaresum(4*i+3): print td+'总',
else: print td+'--',
print ppfactorint(4*i+0),
print ppfactorint(4*i+1),
print ppfactorint(4*i+2),
print ppfactorint(4*i+3).strip(','),
print