from visual import *
from visual.graph import *

class primeClass:
    def __init__(self,maxN):
##        self.stepGraph = curve(color=color.red, visible=1)
        self.maxNumber = maxN
        self.Ntrials = 0
        self.newBiggestPrime = 1
        self.primes = list()

    def isPrime(self,number):
        if number in self.primes:
            return 1
        for count in xrange(int(sqrt(number))-1):
            divisor = int(sqrt(number))-count
            remainder = number%divisor
            if remainder == 0:
                return 0
        else:
            return 1

    def getBiggestPrime(self,number):
        count = 0
        while count < number:
            if self.isPrime(number-count):
                self.newBiggestPrime = number-count
                if self.newBiggestPrime in self.primes:
                    return self.newBiggestPrime
                else:
                    self.primes.append(self.newBiggestPrime)
                    return self.newBiggestPrime
            count += 1
        return self.newBiggestPrime

    def subPrime(self,number,prime):
        count = number%prime
        self.Ntrials += 1
        if 0 <= count:
            self.newBiggestPrime = self.getBiggestPrime(count)
##            print "New biggest relative prime found as:", self.newBiggestPrime, "for remainder of:", count%self.newBiggestPrime
            if count%self.newBiggestPrime != 0:
                count -= 1
                self.subPrime(count%self.newBiggestPrime,self.newBiggestPrime)
            else:
                count = 0
            
    def iterate(self,start):
        number = start
        while number <= self.maxNumber:
            self.Ntrials = 0
            biggestPrime = self.getBiggestPrime(number)
            if number == biggestPrime:
                pass
##                print "Number:", number, "is prime."
            else:
##                print "Biggest prime less than or equal to number:", number, "is:", biggestPrime
                self.subPrime(number,biggestPrime)
##            print "Number of trials:", self.Ntrials
##            arg = log(number-start+1)
##            dist = arg*1.0+self.Ntrials
            
            if self.Ntrials == 0:
                color = (1.0,1.0,1.0)
            elif self.Ntrials == 1:
                color = (1.0,0.0,0.0)
            elif self.Ntrials == 2:
                color = (0.0,1.0,0.0)
            elif self.Ntrials == 3:
                color = (0.0,0.0,1.0)
            else:
                color = (0.0,0.0,0.0)
                
##            color = (1.0-float(self.Ntrials/sqrt(self.maxNumber)),1.0-float(self.Ntrials/sqrt(self.maxNumber)),1.0-float(self.Ntrials/sqrt(self.maxNumber)))
            length = int(sqrt(self.maxNumber))
            xpos = number%length - length/2.0
            ypos = int(number/length) - length/2.0
##            self.stepGraph.append(pos=(dist*cos(arg),dist*sin(arg)),color=(1.0-float(self.Ntrials/14),1.0-float(self.Ntrials/14),1.0-float(self.Ntrials/14)))
            self.createBox((xpos,ypos,0.0),color)
            number += 1

    def createBox(self,pos,color):
        size = 1/sqrt(self.maxNumber)
        box(pos=pos, length=1.0, height=1.0, width=1.0, color=color)
        
        
startNumber = 0
maxNumber = 10000

primeObject = primeClass(maxNumber)
primeObject.iterate(startNumber)


