Join for FREE | Take the Tour Lost Password?
[x]

deviantART

 
About Me Member Deviant of Many Talents MeyermagicMale/United States Recent Activity Deviant for 2 Years
Needs Premium Membership
Statistics 73 Deviations
48 Comments
1,303 Pageviews

Not sure...

Sat Nov 24, 2007, 2:30 PM
Well, I haven't updated much in a while, partially because I was a bit turned off by dA's new censorship policy (also because most of my recent creative works have been musical, and thus hard to upload). Either way, I got over it ; )

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.

  • Mood: Content
  • Listening to: "Diva" - Mark Mancina, Blood+ OST
  • Reading: Minds!
  • Watching: Clannad, Higurashi no Naku Koro Ni Kai
  • Playing: Upright Bass, Piano
  • Eating: Ramen and Sugar!
  • Drinking: Water! (always)

deviantID

No deviantID yet.

Devious Info

  • Current Residence: San Francisco, California
  • Interests: Math, physics, technology, interesting people, old cartoons, sci-fi, anime, good books, music
  • Favourite genre of music: Varies: <last.fm/user/Meyermagic>
  • Operating System: Fedora 10!
  • Personal Quote: "You can use calculus to not find prime numbers..."
  • Tools of the Trade: Pencil, paper, pens, compass, ruler, eraser, hand, scanner, camera + computer

deviantART Community Board

[x]

Comments


:icon3rdfury:
Hi Meyer! It's Thea.
... 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....
:iconk1re1:
hi!! :wave: thanks so much for the :+fav:!! :hug:

:boogie:

--
"Tengo una soledad tan concurrida, tan llena de nostalgias y de rostros de vos, de adioses hace tiempo y besos bienvenidos."
:iconmeyermagic:
'welcome!

--
"Forethought just isn't doing it for me; I'd rather have afterthought in advance."
:icondetail24:
thanks for your visit :)

--
Dennis M. | visual poetry on detail24.net | prints
:iconmeyermagic:
No problem.

--
"Forethought just isn't doing it for me; I'd rather have afterthought in advance."
:iconbuckskinheart:
Hey, Meyer!!! This is cool- DA unites people =] I look forward to seeing you post more photography. Now I shall go update my own account b/c it's been a shameful lot of time since I last did that.
:iconletrainblu:
It's loren~!

Rejoice at my motivation to post to the web!

--
At the center of the universe, there is a tomato.
:icontownsie:
Thanks for the FAV!!! :)

--
"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
:iconmeyermagic:
Your very welcome!
:iconspecialagentfluffy:
Mwaha, you've been tagg'd. *pokes*

--
Don't mess with me! I have, er... a tiny resin bunny! And I'm not afraid to use him!

Site Map