'''
Created on Mar 20, 2013

@author: user
'''


def even_fibonacci_sum():
    '''
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
'''
    summ = 2; a = 1;b = 2;nxt = 0;
    while nxt <= 4000000:
        nxt = a + b
        if nxt % 2 == 0 : summ += nxt
        a = b
        b = nxt
    print( summ)
    

def largest_prime_factor(n):
    '''The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?'''
    d=2
    factors=list()
    while(n>0):
        while(n%d==0):
            factors.append(d)
            n/=d
        d+=1
        if d*d>n:
            if(n>1): 
                factors.append(n)
                break;
    print (factors[-1])
        

def largest_pallindrom_prodcut_of_three_digit_numbers():
    '''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.'''
    n1=1000 ; n2=999
    p=0
    for n1 in range (999,99,-1):
        for n2 in range(999,99,-1):
            product=n1*n2
            if product<p: break;
            if product>p and str(product)==str(product)[::-1]:
                print ("Gotcha  %d x %d = %d"%(n1,n2,product))
                p=product
                break
            
    print ("%d"%(p))

if __name__ == '__main__':
    largest_pallindrom_prodcut_of_three_digit_numbers()
