Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Monday, August 3, 2009

Greatest prime factor of a number: Project Euler Problem 3 in python

I think this converges fairly fast and is also based on the additive nature of primal factors. Further , it kind of leads into why primes get rarer (and hence this can be use to converge to prime factors)

Algorithm explained
Start with the smallest prime(p=2) and divide the evil number (e)
If it divides, extract he largest power of 2 from e possible.
--The divisor now is divisible by a prime factor greater than 2 and can utmost be e/2^i
--increment p to 3 and so on. Also check if e/3 is less than 3. This forms the outer limit

If the smallest prime (p=2) does not divide the number (e), even better for us
-- The number is divisible by something greater than 2 and hence, the other factor is less than e/2.
-- increment p by 1 and check if less than e/2. redo from 1st step

Heres the program,

# Problem 3 - to find the largest prime factor of a number

n= 600851475143

z=n

i = gcf = 2
while i <>
if n%i == 0:
x = 1
while (n%(i**x)) == 0:
x = x + 1
z =n/(i**(x-1))
n = z
gcf = z
print i," power ",x-1, " and other divisor is ", z
else:
z = n/i
gcf=n
i=i+1

print "The program is complete and the gratest prime factor is ", gcf

Saturday, January 24, 2009

How I maintain my mp3 library using python

I use a combination of iTunes , iTunes updater and Media Monkey . Use media monkey to update the tags, and iTunes updater to read it into iTunes. I use iTunes since I like the interface to listen to my music.


This was after cleaning up with my own scripts for the folders that I inherited. I use a python file MP3Xplor.py to read the directory and the tags into a excel file (using pipe separated txt). I then correct it and send it back using the MP3Change.py file. 
MP3Xplor.py
#!/usr/bin/env python
#coding=utf-8
import ID3Writer
import id3reader

fIn = open('in.txt','r')

sStr= fIn.readline()

while sStr:
lStr = sStr.split("|")


fN = lStr[0]
id3r = id3reader.Reader(fN)
tr = id3r.getValue('track')
try:
tr = int(tr)
except:
tr = 1
art = id3r.getValue('performer')
tit = id3r.getValue('title')
yr = str(id3r.getValue('year'))
lbl = id3r.getValue('label')
ttltr = id3r.getValue('totaltracks')
try:
ttltr = int(ttltr)
except:
ttltr = 1

id3w = ID3Writer.Writer(fN,tr,lStr[2],lStr[3],lStr[1],yr,lbl,ttltr)
sStr = fIn.readline()

MP3Change.py
import os
#from tagger import *
import id3reader
fOut = open ("out.txt", 'w')
mp3attribs = ['album','performer','title','genre']
sStr = ""

for roots,dirs,files in os.walk(r'H:\Karthik\Music\Carnatic Music\Ariyakudi'):
for f in files:
if os.path.splitext(f)[1]=='.mp3':
fN = os.path.join(roots,f)
id3r = id3reader.Reader(fN)
i=0
sStr = sStr + roots+"|"+ f +"|"+fN
while i < len(mp3attribs):
sStr=sStr+"|"+str(id3r.getValue(mp3attribs[i]))
i=i+1
sStr = sStr + "\n"
fOut.write(sStr)
## i3 = ID3v1(fN)
## print roots,"|",f,"|",i3.album,"|",i3.artist,"|",i3.genre