This page is powered by Blogger. Isn't yours? tech

qptech blog

The companion to qpmarl blog. Here you will find all technical related posts (mostly computer and linux stuff)

Monday, October 31, 2005

 

Palindrome primes code

Here's the code. I wrote this while just playing around with python and I never intended it for use in any serious project, so don't critisize the code too much.

This is the code that generates and tests palindromes, printing them if they're prime:


def palinprimes(size=5):
print
linelen=0
len2=size/2
len1=len2+size%2
fdigs="1379"
odigs="0123456789"
omax = len(odigs)
for fn in range(len(fdigs)):
fstr = fdigs[fn]
odone=False
ocount=[0]*(len1-1)
while not odone:
ostr=""
if len1 > 1:
for on in ocount:
ostr = ostr + odigs[on]
index=len(ocount)-1
ocount[index] = ocount[index]+1
while ocount[index] >= omax:
ocount[index]=0
index = index -1
if index < 0:
odone = True
break
ocount[index] = ocount[index]+1
else:
odone = True
numstr=fstr+ostr
for index in range(len1-len2,len1):
numstr = numstr + numstr[len1-index-1]
if primes.primetest(long(numstr)):
linelen=linelen+len(numstr)+1
if linelen>80:
print
linelen=len(numstr)+1
print numstr,


The one parameter that this function takes (defualts to 5) sets the size (in digits) of palindromes to test. It will test all the palindromes of that size. I don't make any guarantees that my function will generate every palindrome, or that it will correctly find the ones that are prime. But I am quite certain that it does both.

Here is the code to test the prime numbers - it's my entire prime test module including 4 functions (not all of which are required for the palindrome function, but I include them here anyway.)



from math import sqrt
primelist=[2,3,5,7]

def addprime(min=1):
candidate=primelist[-1]
if min==1:
min=primelist[-1]+2
while min >= primelist[-1]:
primefound=0
while primefound==0 :
isprime=1
candidate=candidate+2
index=0
while isprime == 1 and primelist[index] ** 2 <= candidate :
if candidate % primelist[index] == 0:
isprime = 0
index = index + 1
if isprime == 1:
primefound = 1
primelist.append(candidate)


def listprimes(start,end):
if primelist[-1] ** 2 < end:
print "Expanding test list"
addprime(int(sqrt(end)))
print
linelen=0
num = start
if num%2 == 0:
num = num + 1
while num <= end:
isprime=1
index=0
while isprime == 1 and primelist[index] ** 2 <= num:
if num % primelist[index] == 0:
isprime = 0
index = index+1
if isprime == 1:
pstr=str(num)
linelen = linelen + len(pstr) + 1
if linelen > 80:
print
linelen=len(pstr) + 1
print pstr,
num = num + 2

def primetest(num):
if num < primelist[-1]:
return primelist.count(num) > 0

if primelist[-1] ** 2 < num:
addprime(int(sqrt(num)))
for x in primelist:
if num % x == 0:
return False
return True

def primesofdigits(maxdigits=5):
addprime(int("9"*maxdigits))
rlist=[]
digits=1
index=0
count=0
while digits <= maxdigits and index < len(primelist):
if len(str(primelist[index])) == digits:
count = count + 1
else:
rlist.append(count)
count = 1
digits = digits + 1
index = index + 1
return rlist



This is my python code, you should be able to copy it into a file named "primes.py" and import it into the python interpreter (import primes). If you have any problems with the code here and can't make any sense of it, just email me (If you have my email address). Or make your request in a comment and I'll see what I can do for you.

This blog layout is not good for code listing, so make of it what you can.

I had the palindrome function in another module, but you should be able to add it to the primes.py module and call it as primes.palinprimes(5) You may have to change "primes.primetest(long(numstr))" to "primetest(long(numstr))" but it should work as is.

 

forget SMB

having trouble networking your windows machines? or maybe a linux machine to a windows machine using Samba? Forget it!

SMB doesn't work anymore. It's bloated with different versions in each version of windows. You can't even network two Windows XP machines running the same version without bashing your head against the wall. It's a pain in the neck.

Just run an anonymous FTP server. Linux, Windows, it doesn't matter. You can connect to it from any computer, you can run it on any computer.

There are many, many ftp clients out there (you can just use windows explorer in a pinch).

There are also many ftp servers to choose from. I am currently using proftp. It works, I don't have any complaints. It's free (open source). Configuration may be a bit tricky, but once the server is setup, connecting to it is foolproof.

For configuration, look for some howto's on the internet or something (I didn't find any very good ones, but I figured out enough on my own (and with the proftp man pages) to get it working)

If you are running an ftp server that is visible on the internet, you will want to read about the security concerns that are involved with running such a setup. (it's probably safer than using any of those peer-to-peer networks out there).

Next time: run your own http server.