[Tutor] project euler prime factorization problem
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? #don't forget 2,3,5,7. this function doesn't deliver those as output. def is_prime(b): #checks a number greater than 7 to see if it is prime and returns if is. if b % 2 != 0 and b % 3 != 0 and b % 5 != 0 and b % 7 != 0: print b, def factor(num): x = num b = 1 c = num while b <= c: #starts at 1 and searches all the numbers up to the number you put in if x % b == 0: is_prime(b) b += 1 else: b += 1 print "Don't forget to consider primes 2, 3, 5, and 7\n" #600851475143 #shows what numbers in given range are prime ''' def is_prime(b): return [2,3,5,7] + [x for x in xrange(3, 1, 2) if x % 2 != 0 and x % 3 != 0 and x % 5 != 0 and x % 7 != 0] ''' I'm looking for some help with this problem. I realize my code is inefficient for such a big number, and also I'm not quite sure it works perfectly in and of itself. My current reasoning was something of this sort: Find all the factors of a number, then reduce them to just the prime factors and you can then multiply them together to get that number thus having the prime factors. I need a lot of help haha. Thanks in advance everyone. If anyone has a good resource to point me to other than the open book project and dive into python it would be much appreciated. Would it be useful for me to buy a book, and if so what are some easily accessible ones? I feel dive into python is just too advanced for me. I understand a lot of the teachings, but the examples seem unwieldy and esoteric. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] project euler prime factorization problem
"Nick" wrote What is the largest prime factor of the number 600851475143 ? For help on the math aspects try Wikipedia. Look up Prime factors... Would it be useful for me to buy a book, and if so what are some easily accessible ones? I feel dive into python is just too advanced for me. I understand a lot of the teachings, but the examples seem unwieldy and esoteric. If you have programmed before in any language just use the standard Python tutorial. If not choose one of the several tutorials on the Non Programmes beginners page. Without knowing more about your personal needs and objectives its impossible to be any more specific. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] more on wx and tiff
Hi, Got it. Thought it would be nice to share it. What set me on the wrong foot completely was that writing gif files is not possible due to copyright issues. Below is a very simple version of a conversion function for Group 4 Tif to png. import wx, os, time def g4TifToPng(tif, png = None): if png is None: png = os.path.splitext(tif)[0] + ".png" myImg = wx.Image(tif) if myImg.GetImageCount(tif) == 2: SECONDPAGE = 1 myImg = wx.Image(tif, type = wx.BITMAP_TYPE_TIF, index = SECONDPAGE) # myImg = myImg.GetSubImage((0, 2338, 3304, 2338)) # offset h & w, rect h & w myImg.SaveFile(png, wx.BITMAP_TYPE_PNG) newW, newH = myImg.GetSize() print "%s:: writing file %s (%s x %s pixels)" % \ (time.strftime("%H:%M:%S"), png, newW, newH) g4TifToPng(tif = "c:/temp/somefile.tif") Cheers!! Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ From: Albert-Jan Roskam To: Wayne Werner Cc: Python Mailing List Sent: Fri, August 27, 2010 8:00:16 PM Subject: Re: [Tutor] more on wx and tiff Hi Wayne, Yep, I considered using PIL, but that package won't read so-called Group 4 Tiffs [1]. They're two-page, black-and-white scanned forms. I need part of the second form (i.e., the backside of a certificate). The page contains some hand-written info which needs to be presented in a simple data entry program, which I made using Tkinter. The forms are confidential so they may not leave the company network. And the IT droids are somewhat paranoid so it's very time-consuming to get some new executable 'inside' the company network. Beeeh, formalities... :-( [1] http://www.digitalpreservation.gov/formats/fdd/fdd24.shtml Cheers!! Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ From: Wayne Werner To: Albert-Jan Roskam Cc: Python Mailing List Sent: Fri, August 27, 2010 6:44:04 PM Subject: Re: [Tutor] more on wx and tiff On Fri, Aug 27, 2010 at 11:25 AM, Albert-Jan Roskam wrote: Hi again, > >Some more questions about tiff conversion. > >First, thanks for your previous replies. I cannot use IrfanView any time soon, >nor will my boss switch to Linux. > > Have you tried using the PIL? http://www.pythonware.com/products/pil/ import Image i = Image.open("file.tiff") i.save(open('file.png', 'w'), filetype='png') I don't know if that was previously suggested, but it should work on any platform with PIL installed. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Adding to a CSV file?
Hi, I'm learning Python so I can take advantage of the really cool stuff in the Natural Language Toolkit. But I'm having problems with some basic file manipulation stuff. My basic question: How do I read data in from a csv, manipulate it, and then add it back to the csv in new columns (keeping the manipulated data in the "right row")? Here's an example of what my data looks like ("test-8-29-10.csv"): MyWord Category Ct CatCt ! A 2932 456454 ! B 2109 64451 a C 7856 9 a A 19911 456454 abnormal C 174 9 abnormally D 5 7 cats E 1999 886454 cat B 160 64451 # I want to read in the MyWord for each row and then do some stuff to it and add in some new columns. Specifically, I want to "lemmatize" and "stem", which basically means I'll turn "abnormally" into "abnormal" and "cats" into "cat". import nltk wnl=nltk.WordNetLemmatizer() porter=nltk.PorterStemmer() text=nltk.word_tokenize(TheStuffInMyWordColumn) textlemmatized=[wnl.lemmatize(t) for t in text] textPort=[porter.stem(t) for t in text] # This creates the right info, but I don't really want "textlemmatized" and "textPort" to be independent lists, I want them inside the csv in new columns. # If I didn't want to keep the information in the Category and Counts columns, I would probably do something like this: for word in text: word2=wnl.lemmatize(word) word3=porter.stem(word) print word+";"+word2+";"+word3+"\r\n") # Looking through some of the older discussions about the csv module, I found this code helps identify headers, but I'm still not sure how to use them--or how to word the for-loop that I need correctly so I iterate through each row in the csv file. f_out.close() fp=open(r'c:test-8-29-10.csv', 'r') inputfile=csv.DictReader(fp) for record in inputfile: print record {'Category': 'A', 'CatCt': '456454', 'MyWord': '!', 'Ct': '2932'} {'Category': 'B', 'CatCt': '64451', 'MyWord': '!', 'Ct': '2109'} ... fp.close() # So I feel like I have *some* of the pieces, but I'm just missing a bunch of little connections. Any and all help would be much appreciated! Tyler ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] project euler prime factorization problem
On 8/29/2010 3:08 AM, Nick wrote: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? #don't forget 2,3,5,7. this function doesn't deliver those as output. def is_prime(b): #checks a number greater than 7 to see if it is prime and returns if is. if b % 2 != 0 and b % 3 != 0 and b % 5 != 0 and b % 7 != 0: print b, def factor(num): x = num b = 1 c = num while b <= c: #starts at 1 and searches all the numbers up to the number you put in if x % b == 0: is_prime(b) b += 1 else: b += 1 print "Don't forget to consider primes 2, 3, 5, and 7\n" #600851475143 #shows what numbers in given range are prime ''' def is_prime(b): return [2,3,5,7] + [x for x in xrange(3, 1, 2) if x % 2 != 0 and x % 3 != 0 and x % 5 != 0 and x % 7 != 0] ''' I'm looking for some help with this problem. I realize my code is inefficient for such a big number, and also I'm not quite sure it works perfectly in and of itself. Did you test the program? That is one way to tell whether it works perfectly. What you showed above will do one visible thing - it will print "Don't forget to consider primes 2, 3, 5, and 7\n". The rest is a somewhat confusing collection of function definitions and comments. You never call the functions so nothing else will happen. As Alan said - research prime factors to see how others approach it. My current reasoning was something of this sort: Find all the factors of a number, then reduce them to just the prime factors Very inefficient. IMHO the proper way is to generate a list of all the prime numbers up to the square root of 600851475143, then test each (starting with the largest and working down) till you discover a factor. That then is the answer. There are many published algorithms for generating primes. and you can then multiply them together to get that number thus having the prime factors. I need a lot of help haha. Thanks in advance everyone. If anyone has a good resource to point me to other than the open book project and dive into python it would be much appreciated. Would it be useful for me to buy a book, and if so what are some easily accessible ones? I feel dive into python is just too advanced for me. I understand a lot of the teachings, but the examples seem unwieldy and esoteric. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] project euler prime factorization problem
"Did you test the program? That is one way to tell whether it works perfectly. What you showed above will do one visible thing - it will print "Don't forget to consider primes 2, 3, 5, and 7\n". The rest is a somewhat confusing collection of function definitions and comments. You never call the functions so nothing else will happen. As Alan said - research prime factors to see how others approach it. My current reasoning was something of this sort: Find all the factors of a number, then reduce them to just the prime factors Very inefficient. IMHO the proper way is to generate a list of all the prime numbers up to the square root of 600851475143, then test each (starting with the largest and working down) till you discover a factor. That then is the answer. There are many published algorithms for generating primes. and you can then multiply them together to get that number thus having the prime factors. I need a lot of help haha. Thanks in advance everyone. If anyone has a good resource to point me to other than the open book project and dive into python it would be much appreciated. Would it be useful for me to buy a book, and if so what are some easily accessible ones? I feel dive into python is just too advanced for me. I understand a lot of the teachings, but the examples seem unwieldy and esoteric. -- Bob Gailer " Yeah, thanks everyone for the comments. I will follow your advice Bob. I didn't call the functions in the program because I was calling them myself in the interpreter after running it. I don't know if that is the way everyone else programs, but I just write it in idle and save it and run it over and over making sure it is doing what I want ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] project euler prime factorization problem
On Mon, 30 Aug 2010 05:31:00 am bob gailer wrote: > > My current reasoning was something of this sort: Find all the > > factors of a number, then reduce them to just the prime factors > > Very inefficient. IMHO the proper way is to generate a list of all > the prime numbers up to the square root of 600851475143, then test > each (starting with the largest and working down) till you discover a > factor. That then is the answer. Actually your approach is inefficient too, and it won't always work. Consider what happens if you are asked for the prime factors of 101. You would generate the primes up to 10: 2, 3, 5, 7 but none of those are factors. In the case of 600851475143, you wastefully generate 62113 prime numbers when you actually only need 224. The right way is to start at 2, then 3, and so forth, working up rather than down. Don't forget that there can be repeated factors. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] project euler prime factorization problem
"Nick" wrote I didn't call the functions in the program because I was calling them myself in the interpreter after running it. I assume you mean after importing it? Running a program is generally taken to mean executing the script as a standalone program. To execute the internal functions one has to import the file as a module. ...know if that is the way everyone else programs, Its a good way of developing code because you get instant feedback but its not a comprehensive or reliable way to test code, for that you are better creating a set of systematic tests checking upper and lower boundaries as well as midrange and out of range values plus various types of invalid inputs. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] project euler prime factorization problem
Steven D'Aprano wrote: On Mon, 30 Aug 2010 05:31:00 am bob gailer wrote: My current reasoning was something of this sort: Find all the factors of a number, then reduce them to just the prime factors Very inefficient. IMHO the proper way is to generate a list of all the prime numbers up to the square root of 600851475143, then test each (starting with the largest and working down) till you discover a factor. That then is the answer. Actually your approach is inefficient too, and it won't always work. Consider what happens if you are asked for the prime factors of 101. You would generate the primes up to 10: 2, 3, 5, 7 but none of those are factors. I agree about inefficient, but not "won't always work." If you've got a list of all the primes up to the square root of n, and none of them are factors, then n is prime as well. Not necessarily the next prime in the list, but prime nonetheless. So perhaps you're objecting that Bob didn't say the else-portion of his algorithm - He said "till you discover a factor" but neglected to say what it means if you don't. If you don't, then the value 600851475143 is prime. So the number would be its own largest prime factor. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding to a CSV file?
On 08/29/2010 02:12 PM, aenea...@priest.com wrote: > > Hi, > > I'm learning Python so I can take advantage of the really cool stuff in the > Natural Language Toolkit. But I'm having problems with some basic file > manipulation stuff. > > My basic question: How do I read data in from a csv, manipulate it, and then > add it back to the csv in new columns (keeping the manipulated data in the > "right row")? > > Here's an example of what my data looks like ("test-8-29-10.csv"): Python has a great module in the standard library - csv. It's really easy to use. http://docs.python.org/library/csv.html Greg ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor