#! /usr/bin/env python

import math
import time
import commands

#path='/usr/local/httpd/htdocs/splintergroup/rdrink/prime_search/'
f=open('/usr/local/httpd/htdocs/splintergroup/rdrink/prime_search/primes16.txt','a')
g=open('/usr/local/httpd/htdocs/splintergroup/rdrink/prime_search/unprimes16.txt','a')

def factor(n):
#-- Improved function for factoring numbers --#
	startTime=time.time()
	limit = int(math.sqrt(long(n)))			#only search up to sqr-root
	m = limit/2					#start in the middle
	i=0
	factor=0
	
	while (m >=2 and m <= limit):
		i=abs(i)+1			#increment i, disregarding sign
		if (i%2 == 0): i=i* -1		#swap sign of odd numbers to negative
		m=m+i				# work from middle forward and back
		
		if(n%m == 0): 
			break			#break on factor
			
	if(m==1):
		return [1,n,abs(i),startTime]	#return 1 if prime + steps
	else:
		return [0,n,abs(i),startTime]	#return 0 if not prime + steps
		
def web(num,steps,start,total,sTime,prec,count,end):
	
	diff=time.time()-sTime
	diffPart=time.localtime(diff)
	diffTime= ' %dm:%ds' % (diffPart[4],diffPart[5])

	h=open('/usr/local/httpd/htdocs/splintergroup/rdrink/prime_search/prime_search.inc','w')
	
	content='<p />Last prime found <br /> <b> '+str(num)+'</b>\n<p />The last number checked [#'+str(count)+' of '+str(end)+'] factored in '+str(steps)+' calculations in '+diffTime+'\n'
	foot='<p /><i>Started on '+start+'<br />'+str(total)+' total calculations to date ('+str(prec)+'% done)</i>'
	
	h.write(content+foot)
	h.flush()
	h.close()


# sixteen-digit prime list #

#-- NOTE: some browsers seem to have a limit on how many character can be displayed on one line, --#
#-- if you do not see the array primeList() below, and you want to use this script in Python,    --#
#-- you will need to add the array primeList() and fill it with these eight-digit numbers        --#
#-- http://www.splintergroup.com/rdrink/prime_search/primes8.txt                                 --#

#-- the array primeList() goes here --#

primeList=(13191361,13191979,13193137,13193797,13194373,13196719,13199743,13611319,13611979,13613137,13613767,13613779,13616737,13616761,13671319,13671373,13673137,13673719,13674373,13677919,13679767,13731931,13731973,13736113,13736143,13737331,13737937,13739767,17231783,17231789,17232389,17232953,17234783,17237159,17238329,17411747,17411753,17411783,17412347,17412371,17412953,17412971,17414729,17418329,17418923,17471723,17472971,17474729,17474783,17477129,17531741,17531783,17532917,17534723,17534729,17534789,17537159,17538317,17538923,17538929,17538971,17591723,17592371,17592389,17594153,17594789,17598353,17598941,17832953,17835347,17837129,17837159,17894153,17895953,17898317,17898329,17898941,17898971,19131361,19131367,19131373,19133167,19133761,19133797,19134397,19136113,19136197,19136779,19137331,19137919,19311913,19311979,19313167,19316173,19316197,19316719,19316779,19319767,19731979,19731997,19733797,19734397,19736113,19736131,19736173,19737331,19737937,19739719,19791319,19791367,19791973,19796173,19796737,19971373,19971979,19973119,19973137,19974397,19976113,19976779,23411747,23412371,23414723,23417129,23471741,23472341,23472389,23475323,23475953,23477159,23478317,23478941,23712383,23712917,23714153,23714729,23714759,23715347,23715953,23718353,23718971,23831747,23832917,23832953,23834159,23837129,23838317,23838389,23891753,23894729,23894789,23897129,23898353,29171789,29172371,29172389,29174129,29174723,29175347,29175953,29177129,29531741,29531753,29531783,29532389,29532917,29534129,29534159,29535347,29538317,29538329,29538941,29711741,29711789,29714129,29715347,29715953,29718971,31191373,31191913,31191997,31193137,31193167,31193761,31193797,31196197,31196719,31199719,31371979,31373761,31373779,31374397,31376113,31377331,31377919,31379743,31379767,31671319,31671361,31673797,31674373,31676719,31676737,37191997,37193119,37194337,37196113,37196779,37197331,37199719,37611373,37611997,37613167,37613767,37614397,37616197,37619719,37619767,37671367,37671973,37671979,37673761,37676143,37676173,37676719,37793167,37794373,37799719,37799767,37971979,37976131,37977937,41295923,41297159,41298329,41298353,41531753,41532341,41532971,41534789,41535323,41538923,41538941,41591723,41591783,41591789,41592371,41592389,41594723,41594783,41594789,41595347,43371373,43373761,43373767,43373779,43373797,43376131,43376761,43731367,43731931,43731997,43733119,43733719,43733761,43733797,43734337,43736779,43737919,43971973,43971997,43973119,43973719,43976131,43976719,43977331,47231747,47234153,47234783,47235323,47237129,47238923,47291723,47291789,47294759,47298941,47591723,47591759,47591783,47594783,47598389,47831723,47834729,47835323,47838353,47891741,47892347,47894153,47894159,47895347,47898329,47898353,53231723,53232917,53234729,53235953,53238329,53238971,53472383,53474153,53474723,53475923,53478941,59231747,59234159,59234759,59237159,59238929,59531741,59531789,59532383,59532971,59534159,59534723,59534729,59534789,59538923,61131319,61131979,61133167,61134373,61311361,61311931,61311973,61313137,61313167,61313719,61313761,61316737,61316779,61319743,61431373,61433167,61433719,61433761,61436779,61439743,61733761,61734373,61737331,61737919,61739719,61971997,61973167,61973767,61973797,61976779,61979719,67191373,67191931,67193767,67196761,67197919,67199719,67371361,67371973,67376143,67376173,67376779,67379743,67611931,67613719,67613779,67616173,67617331,67791319,67793137,67796719,67796737,67796761,71291741,71291747,71291783,71292371,71292383,71292953,71295347,71298923,71591759,71591789,71592971,71594129,71594723,71594729,71598353,71598929,71598971,73313137,73313797,73316143,73316197,79191319,79191361,79191367,79191913,79191997,79193119,79193137,79194373,79194397,79371373,79371913,79371979,79373167,79373719,79373797,79376113,79376719,79376761,83174159,83174759,83174783,83175923,83291723,83291783,83292917,83294153,83295923,83298389,83298923,83531741,83532971,83534723,83535923,83537159,83538929,83898923,83898929,89232383,89232953,89234723,89234729,89235323,89235347,89235953,89238971,89291723,89291747,89291753,89291789,89294159,89294759,89295923,89298389,89298971,89411747,89411759,89411783,89412383,89412971,89414159,89414723,89417129,89711723,89717129,89718353,97191319,97193137,97196173,97199743,97433719,97433779,97434373,97436113,97437919,97437937,97671319,97671367,97673137,97676113,97676197)

##-- Get state of previous run from log --##
        
l=open('/usr/local/httpd/htdocs/splintergroup/rdrink/prime_search/state.txt','r')
state=[]
for i in range(4):
	value= l.readline()
        value=value.replace('\n','')
        state.append(value)

c=int(state[0])
r=int(state[1])

listLen=len(primeList)
totalCombos=listLen**2

#c=96				# column number, set on restart
#r=0				# row number
startPoint=c+(r*listLen)	# count by colunm and row


#started= time.asctime(time.localtime(time.time()))
started='Wed Nov 9 23:54:09 2005'

#lastPrime=1319136113679767L
#grandTotal=long(2115975816)

lastPrime=long(state[2])
grandTotal=long(state[3])


### Find primes  ###

for p in range(startPoint, totalCombos):	

        precent=101*float(p)/totalCombos

##-- factor --##
	searchNum=long(str(primeList[r])+str(primeList[c]))

	result = factor(long(searchNum))

        grandTotal=grandTotal+result[2]
	if(result[0] == 1): 
		lastPrime=result[1]

##-- write to web --##

        web(lastPrime,result[2],started,grandTotal,result[3],precent,p,totalCombos)

##-- write to files --##

	if(result[0] == 1):
		f.write(str(result[1])+'\n')			#write to file
		f.flush()
	else:							#not prime
		g.write(str(result[1])+'\n')
		g.flush()

#-- increment column (c) and row (r) --#

	c=p%listLen
	if(c==0):r=r+1

#-- log state --#

	s=open('/usr/local/httpd/htdocs/splintergroup/rdrink/prime_search/state.txt','w')
	s.write(str(c)+'\n'+str(r)+'\n'+str(lastPrime)+'\n'+str(grandTotal)+'\n\n#column\n#row\n#lastPrime\n#grandTotal')
	s.flush()
	s.close()

#-- finish ==#

f.close()
g.close()	

