#!/bin/python3
import cgi
import cgitb; cgitb.enable()
import math
def isprime(ournum):
 ii=2
 while ii<=math.sqrt(ournum):
  if (ournum%ii==0):
   return False
  ii+=1
 return True
print ("Content-Type: text/html")
print ()
maxnum=cgi.FieldStorage().getvalue("maxnum")
print ("<html>\n<head>\n<title>PrimeSpiral</title>\n</head>\n<body>")
if (maxnum==None):
 print ("<form action=\"primesp.cgi\"><input type=\"number\" required placeholder=\"The program should generate the prime list until that\" name=\"maxnum\"/><input type=\"submit\"/></form>")
 exit (1)
maxnum=int(maxnum)
primes=list(range(2, maxnum))
primes2=[]
for index in primes:
 if (isprime(index)==True):
  primes2.append(index)
primes=primes2
print ("<svg xmlns=\"https://w3.org/2000/svg\" style=\"object-fit: contain; object-position: center; width: 100%; height: 100%; margin: auto; display: block;\">")
l=0
m="+"
xcoor=0
ycoor=0
svgs=[]
ourMap={"+":"-","-":"+"}
while l < len(primes):
 if (l%2==0):
  if (l!=0):
   m=ourMap[m]
  pass
  if (str(m)=="+"):
   xcoor=xcoor+primes[l]
  else:
   xcoor=xcoor-primes[l]
 else:
  pass
  if (str(m)=="-"):
   ycoor=ycoor-primes[l]
  else:
   ycoor=ycoor+primes[l]
 svgs.append([str(xcoor+600), str(ycoor+600)])
 l+=1
lines=""
o=0
while o < len(svgs)-1:
 lines+="<line x1=\""+svgs[o][0]+"\" y1=\""+svgs[o][1]+"\" x2=\""+svgs[o+1][0]+"\" y2=\""+svgs[o+1][1]+"\" style=\"stroke:black;stroke-width:2\"/>\n"
 o+=1
print(lines+"</svg>\n</body></html>")
