存档

文章标签 ‘Algorithm’

01

接着扫四道水题
#4 Find the largest palindrome made from the product of two 3-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.

#!/usr/bin/env python

biggest = 0

def isPalindromic(num):
    num_str = str(num)
    num_halflen = len(num_str)/2
    for idx in range(0, num_halflen):
        if num_str[idx] != num_str[-(idx+1)]:
            return False
    return True

for x in range(100, 999):
    for y in range(100, 999):
        if isPalindromic(x*y) and x*y > biggest:
            biggest = x*y

print biggest

#5 What is the smallest number divisible by each of the numbers 1 to 20?

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

#!/usr/bin/env python

def isPrimeUnder20(num):
    for tmp in range(2, num):
        if num%tmp == 0:
            return False
    return True

factor_list = []

for x in range(2, 20):
    if isPrimeUnder20(x):
        power_x = 1
        while x**power_x <= 20:
            power_x += 1
        factor_list.append(x**(power_x-1))
        print x,power_x-1

prod = 1
for x in factor_list:
    prod *= x

print prod

#6 What is the difference between the sum of the squares and the square of the sums?

The sum of the squares of the first ten natural numbers is,
1^(2) + 2^(2) + ... + 10^(2) = 385

The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^(2) = 55^(2) = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

#!/usr/bin/env python

sum_of_square = 0
for x in range(1, 101):
    sum_of_square += x**2

sum_tmp = 0
for x in range(1, 101):
    sum_tmp += x
square_of_sum = sum_tmp**2

delta = square_of_sum - sum_of_square
print delta

, ,

30

好久没有动这个了,今天再扫一道水题。

Find the largest prime factor of a composite number.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

题目的数据规模很小,所以可以直接偷懒穷举,so…

#!/usr/bin/env python  

num = 600851475143
seg_size = 10000
base_factor = 0
prime_factors = []

while base_factor*seg_size < num:
    for tmp in range(seg_size*base_factor, seg_size*(base_factor+1)):
        if tmp>1 and num%tmp == 0:
            prime_factors.append(tmp)
            while num%tmp == 0:
                num = num / tmp
            print "num:",num," ",tmp
        base_factor = base_factor + 1

print prime_factors

, ,

14

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, …
Find the sum of all the even-valued terms in the sequence which do not exceed four million.

这道题也很水,所以不多说了…

#!/usr/bin/env python

sum = 0
num1 = 0
num2 = 1
while num2 <= 4000000:
    print num2
    if num2 % 2 == 0:
        sum += num2
    temp = num2
    num2 = num1+num2
    num1 = temp
print sum

, ,

12

接触Python一年多了,但一直没有用它写过什么正经的东西。刚好这个暑假暂时没法回家,在学校也不是特别忙,就把之前觊觎已久的Project Eular翻出来,准备用Python扫一遍,就当作学习Python了。不过以我可怜的水平,不知道能坚持几道…

今天先扫一道超级水题,增强下信心吧……
Project Eular #1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

sum = 0
for num in range(3,1000,3):
    sum = sum+num
for num in range(5,1000,5):
    sum = sum+num
for num in range(15,1000,15):
    sum = sum-num
print sum

, ,