Beatsy
YaBB Newbies
Offline
Posts: 19
Re: PRIME Number Finder
Reply #27 - Dec 2nd , 2009, 2:15pm
Hi I have decided to post the source code and it would be great if people could given suggestions as to how to make it better. PrintWriter output; float MTM=0; String TM; int [] primes=new int[1]; float PR; int x=3; int view=0; int lastprime; PFont font; boolean prime=true; int veiwW=0; int savergap=100; int maxpersave=savergap; int primeslength1; int pps; color alert=color(0,255,0); float framerate=60; int starttime; int time; String [] times=new String[2]; void setup() { size(720,220); background(0); font = loadFont("Algerian-48.vlw"); textFont(font, 48); text("Loading...",40,50); delay(5); times= loadStrings("time.txt"); starttime=int(times[0]); primes= int(loadStrings("primes.txt")); if(primes.length>3) { x=int(primes[primes.length-1]); } else { x=2; } maxpersave=primes.length+savergap; primeslength1=primes.length; frameRate(framerate); output = createWriter("data/primes.txt"); int counter=0; while(counter<primes.length) { PR+=primes[counter]; PR=PR/2; output.println(str(primes[counter])); counter++; } } void draw() { time=millis()/1000; if (primes.length>3) { PR=(PR+(primes[primes.length-1]-primes[primes.length-2]))/2; } delay(int(frameRate/5)); setup_primes(); test_primes(); background(0); rect(0,height-15,width*((x%1000000)/1000000.0),15); if(veiwW==0) { display_primes(); } if(veiwW==1) { view_primes(); } if(veiwW==2) { view_stats(); } if(veiwW==3) { view_clock(); } veiwW=veiwW%4; save_primes(); delay(5); } void keyPressed() { if (keyCode==ESC) { save_primes(); } if(key==' ') { keyPressed=false; veiwW++; } } void setup_primes() { if ((primes.length>maxpersave-2)||((primes.length==maxpersave))) { alert=color(255,0,0); } if (x>2147483647) { exit(); } x++; x++; prime=true; fill(color(alert)); ellipse(width-50,50,21,21); fill(255); } void test_primes() { int counter; int countdif; counter=0; if (x%2==0) { x--; } while((primes.length>counter)&&(primes[counter]<sqrt(x+1))) { countdif=x%primes[counter]; counter++; if (countdif==0) { prime=false; } } if ((prime)&&(x%2==1)) { primes=append(primes,x); output.println(str(x)); lastprime=x; } } void display_primes() { pps=int((primes.length-primeslength1)/(frameCount/frameRate)); text("last prime:"+lastprime,20,60); text("checking:"+x+" PPS:"+int(pps),20,120); text("Found Primes:"+primes.length,20,180); } void save_primes() { if ((primes.length>maxpersave)||(keyPressed)) { maxpersave=primes.length+savergap; output.flush(); alert= color(0,255,0); times[0] = str(int(time+starttime)); times[1] = int((time+starttime)/3600.0)+":"+int((time+starttime)/60.0%60)+":"+int((time+sta rttime)%60); saveStrings("data/time.txt", times); delay(500); } if ((time+starttime)%30==0) { times[0] = str(int(time+starttime)); times[1] = int((time+starttime)/3600.0)+":"+int((time+starttime)/60.0%60)+":"+int((time+sta rttime)%60); saveStrings("data/time.txt", times); } } void view_primes() { view=view%primes.length; view=(view+primes.length)%primes.length; text(primes[view],40,150); text("Prime No# "+view%primes.length,40,50); text("of "+primes.length,40,100); rect(width-30,float(view)/float(primes.length)*height,20,20); if ((mousePressed)&&(mouseX>width-30)&&(mouseX<width-10)) { view=int(float(mouseY)/float(height)*primes.length); } if ((keyPressed)&&(keyCode==UP)) { keyPressed=false; view--; } if ((keyPressed)&&(keyCode==DOWN)) { keyPressed=false; view++; } } void view_stats() { MTM=(1000000-(x%1000000))/20.0/60; TM=str(int(MTM/60)+hour()%12)+":"+str(int(((MTM/60.0%1)*60)%60)); text("TNM"+TM+" PTS: "+((100+primes.length-maxpersave)%100),20,50); text( "T:"+int((time+starttime)/3600.0)+":"+int((time+starttime)/60.0%60)+":"+int((tim e+starttime)%60),440,50); text("FPS:"+int(frameRate)+" Frame Speed: "+int((frameRate/framerate)*100.0)+"%",40,100); text("Prime Rate:"+int(PR)+" APR:"+int(10*float(lastprime)/float(primes.length))/10.0,40,150); } void view_clock() { MTM=(1000000-(x%1000000))/40.0/60; TM=str(int(MTM/60))+":"+str(int((MTM/60%1)*60))+":"+str(int(((MTM/60%1)*3600)%60 )); text("HTM:"+TM,20,50); translate(2*width/3,height/2); fill(255); pushMatrix(); rotate(-PI/2.0); ellipse(0,0,210,210); fill(0); ellipse(0,0,200,200); int count=0; fill(255); while(count<12) { pushMatrix(); rotate(count/12.0*TWO_PI); rect(90,-3,-10,6); if(count/12*4%1==0) { rect(90,-4,-10,8); } if(count==0) { rect(90,-4,-15,8); } count++; popMatrix(); } noStroke(); pushMatrix(); rotate(((MTM/60/60/12%1)*60)%60*TWO_PI); fill(0,255,0); rect(0,-2.5,50,5); popMatrix(); pushMatrix(); rotate(((MTM/3600%1)*60)%60*TWO_PI); fill(0,255,0); rect(0,-2.5,90,5); popMatrix(); pushMatrix(); rotate(((MTM/60%1)*60)%60*TWO_PI); fill(0,0,255); rect(0,-1.5,90,3); popMatrix(); popMatrix(); } thanks