샤브의 블로그 RSS 태그 관리 글쓰기 방명록
Device & Language/Python (6)
2017-08-24 13:17:05

Sum square difference

Problem 6 

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.


Answer 25164150


Code

num = 100

temp = 0

diff = 0


temp =  num ** 2

diff = (3 * temp ** 2 + 2 * temp * num - 3 * temp -2 * num )/12


print(diff)

2017-08-16 20:42:36

Smallest multiple

Problem 5 

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 positive number that is evenly divisible by all of the numbers from 1 to 20?


Answer 232792560


Code

def gcd(a, b):

    while b != 0:

        a, b = b, a % b

    return a


def lcm(a, b):

    return a*b/gcd(a, b)


lcmNum = 1

for i in range(2,21):

    lcmNum = lcm(lcmNum,i)

    

print(lcmNum)

2017-08-16 20:20:34

Largest palindrome product

Problem 4 

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.


Answer

number = 906609


Code

print(max([x*y for x in range(999,900,-1) for y in range(x,900,-1) if str(x*y) == str(x*y)[::-1]]))

2017-04-06 20:47:16

Largest prime factor
Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

Answer
number = 600851475143

i = 2
m = 2
while number != 1:
    if number % i == 0:
        number = number / i
        m = i
    i = i + 1

print m

2014-12-27 21:21:01

Problem : Even Fibonacci numbers
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.

Answer
before =1
now=0
next =0
sum=0
while now < 4000000:
  next = now + before
  before = now
  now = next
  if (now % 2) ==0:
    sum= sum + now
print (sum)

Other
p = 1
n = 2
s = 0
while s < 1000000:
  s += n
  p, n = 2 * n + p, 3 * n + 2 * p
print s


출처 : https://projecteuler.net/problem=2

2014-12-23 21:11:35

Problem : Multiples of 3 and 5
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.

Answer
s=0
for x in range(0, 100):
if (x % 3)==0 :
s = s+x
elif (x %5)==0:
s = s+x
print (s)

Other
sum([x for x in range(1000) if x % 3== 0 or x % 5== 0])

출처 : https://projecteuler.net/problem=1