So, I have been working more on my coding, and finished a new version of AutoCalc, a little SL gadget which identifies and simplifies mathematical expressions in your chat, a prime finding program in Python (highly optimized, but still based on a naive algorithm). Hmm... I think I'll paste the source into my journal:
#####################################################
#Prime Finder - By Meyer S. Jacobs #
#####################################################
#Global Variables and Startup Routines:
import profile
from itertools import izip
from nzmath import *
primes = [2,3,5]
#Functions
def is_prime(test):
for i in primes:
if i <= test ** 0.5:
if not(float(test) % float(i)):#If there is no remainder
return False#Then its composite
else:
return True
print 'Error checking for primality'
def find_primes_count(count_limit):
f_count = len(primes)
n = primes[len(primes) - 1]
while f_count < count_limit:
#All primes other than 2 and 3 follow the form 6k +/- 1
#5.7...11.13...17.19...23.(25)...29.31...(35).37
#or
#(5+2=7, 7+4=11), (11+2=13, 13+4=17), (17+2=19, 19+4=23)
#So, we increment by 2 and 4, starting with [2,3,5] as known primes
n += 2
if is_prime(n):
f_count += 1
primes.append(n)
n += 4
if is_prime(n):
f_count += 1
primes.append(n)
return True
def find_primes_block(block_size):
#Initialize an array (numerical keys, 1/0 values)
block = dict([(x + 2, 1) for x in range(block_size - 1)])
for k, v in block.iteritems():#Iterate through items
if(v):#and if the value is 1 (the number is thought to be prime)
for i in range(long(round((block_size / k) - 0.5) - (k - 1))):
#Loop through and 0 the values for its multiples
block[(i + k) * k] = 0
#Flip the dictionary and get the primes
fp = list()
for k, v in block.iteritems():
if v:
fp.append(k)#Then append them to a list
return fp#and return it
def output_primes(num):
for i in range(len(primes)):
if i < num:
print str(i + 1) + " : " + str(primes[i])
else:
return True
def save_primes():
f = open('list.txt', 'w' )
it = iter(primes)
f.write("\n".join(["%s" % i for i in izip(it)]))
f.close()
#Implementation
cont = True
while cont:
mode = str(raw_input('Algorithm? ("block" or "count"): ' ))
if mode == 'count':
count = long(raw_input('Number of primes to find: ' ))
find_primes_count(count)
output_primes(count)
save = int(raw_input('Save results to file? (1 or 0): ' ))
if save:
save_primes()
elif mode == 'block':
limit = long(raw_input('Block size: ' ))
primes = find_primes_block(limit)
output_primes(len(primes))
save = int(raw_input('Save results to file? (1 or 0): ' ))
if save:
save_primes()
cont = int(raw_input('Continue? (1 or 0): ' ))
if cont:
if mode == 'count':
clear = int(raw_input('Reset prime list? (1 or 0): ' ))
if clear:
primes = [2,3,5]
print 'Bye!'
This software is licensed under the CC-GNU GPL.
Damn emoticons...
Well, I think that'll be it, as the code makes the journal pretty long.







... Yeah. Not much art around here.... love your photos though! Especially "Progress". So where did your slinky go? Haven't seen it around in a while....
--
"Tengo una soledad tan concurrida, tan llena de nostalgias y de rostros de vos, de adioses hace tiempo y besos bienvenidos."
--
"Forethought just isn't doing it for me; I'd rather have afterthought in advance."
--
Dennis M. | visual poetry on detail24.net | prints
--
"Forethought just isn't doing it for me; I'd rather have afterthought in advance."
Rejoice at my motivation to post to the web!
--
At the center of the universe, there is a tomato.
--
"You can't give up on hope coz its hopeless, you gotta hope even more and cover your ears and go blah blah blah...." - Phillip J. Fry
--
Don't mess with me! I have, er... a tiny resin bunny! And I'm not afraid to use him!
Previous Page12Next Page