zhtlancer's blog (English)

alpha...

  • Home
  • Sitemap
  • About
  • zhtlancer’s blog(中文站)
  • RSS Feed

Project Eular #4 #5 #6 (Python)

By zhtlancer on January 30, 2010

Yet another three easy problems.

#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

Posted in Algorithm, Project Eular, Python | Tagged Algorithm, Project Eular, Python | Leave a response

Project Eular #3 (Python)

By zhtlancer on January 30, 2010

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 ?

#!/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

Posted in Project Eular, Python | Tagged Algorithm, Project Eular, Python | Leave a response

Project Eular #2 (Python)

By zhtlancer on January 30, 2010

Project Eular #2

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

Posted in Algorithm, Project Eular, Python | Tagged Algorithm, Project Eular, Python | Leave a response

Project Eular #1(Python)

By zhtlancer on January 30, 2010

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.

#!/usr/bin/env python

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

Posted in Algorithm, Project Eular, Python | Tagged Algorithm, Project Eular, Python | Leave a response

Shortcut keys for BlackBerry qwerty devices

By zhtlancer on January 27, 2010

BlackBerry has implemented plenty of shortcut keys to help improve users’ experience. Also, there are some hidden functions that can only be triggered by special key combination.

There is a collection of Blackberry shortcut keys, which are collected during my BlackBerry addiction time. Hope they are helpful to those who are new to BlackBerry.

Enjoy~

in Message Screen:

alt+s: sms    alt+m:mms    alt+p:call log    alt+i:income message    alt+o: outgoing message    alt+u:toggel read/unread    n/p:next/previous day

home screen(may differs between different themes):

alt+nmll: switch signal strength to number mode

alt+lglg: log viewer
alt+cap+h: system info
alt+cap+del: reset
q: keymaster
r: alarm
t: tasks
u: calculator
i: isms (compose sms)
o: options
p: call log
a: address book
s: search
d: memopad
f: profiles
g: 3gtan
h: program menu
k: keypad lock
l: calendar
x: media
c: compose email
b: browser
m: message
w: connection manager

in service book page:

alt+sbeb: enable service book import

in status page:

buyr: call time
test: run device tests
in sim card page:
mepd

in text input field:

alt+trackball/trackwheel click: copy
left shift+trackball/trackwheel click: paste

in list field:

num+space: prev page
space: next page

Posted in BlackBerry | Tagged BlackBerry | 1 Response

Search

Tags

Algorithm BlackBerry Project Eular Python

Categories

    Archives

    • January 2010

    Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org

    Copyright © 2012 zhtlancer's blog (English).

    Powered by WordPress and Hybrid.