FriendsBlog Archive
|
a couple moreI'm no so proud about this. The eratosthenes function is almost a copy&paste
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
from math import sqrt def eratosthenes (limit): D = {} # map composite integers to primes witnessing their compositeness q = 2 # first integer to test for primality while q <= limit: if q not in D: yield q # not marked composite, must be prime D[q * q] = [q] # first multiple of q not already marked else: for p in D[q]: # move each witness to its next multiple D.setdefault (p + q, []).append (p) del D[q] # no longer need D[q], free memory q += 1 num = 600851475143 print max (filter (lambda x: num % x == 0, eratosthenes (int (sqrt (num)) + 1))) I suspect this one can be more efficient A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. Find the largest palindrome made from the product of two 3-digit numbers.
f = lambda x: x == x[::-1] print max ([ (a * b) for a in xrange (100, 1000) for b in xrange (a, 1000) if f (str (a * b)) ])
Trackback URI: http://www.ceyusa.com/blog/index.php/trackback/520
#1 Re: a couple moreNice codes and algorithms, I starting to think in learning python ... when I work with numbers. [ Reply (0) ]
Leave a Comment
Comment XML feeds: RSS | Atom
|
Recent Comments On Blog