Recent Comments On Blog

rss

a couple more

I'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)))
 
At least I learned a bit more about the prime test and the Eratosthenes sieve
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

# Re: a couple more

linxe, <linxe@glib.org.mx> / 21 August, 10:15pm  
avatar

Nice codes and algorithms, I starting to think in learning python ... when I work with numbers.

Leave a Comment

Write the captcha code you are seeing.

Comment XML feeds: RSS | Atom