본문 바로가기

python

Problem 13 - Find the first ten digits of the sum of one-hundred 50-digit numbers. 링크 Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 ... 53503534226472524250874054075591789781264330331690 이건 그냥 코딩 연습용 문제인듯 python file = open("./data") s = file.read().split("\n") ans = 0 for ss in s: if ss == "": break; ans += int(ss) print str(ans)[:10] 더보기
Problem 12 - What is the value of the first triangle number to have over five hundred divisors? 링크 The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: 1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triang.. 더보기
Problem 11 - What is the greatest product of four numbers on the same straight line in the 20 by 20 grid? 링크 In the 2020 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60.. 더보기
Problem 10 - Calculate the sum of all the primes below two million. 링크 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. python - sieve of Eratosthenes def sieve(n): l = range(n+1) l[1] = 0 sqn = int(round(n**0.5)) for i in range(2, sqn + 1): if l[i] == i: l[2*i:n+1:i] = [i] * (n/i-1) return l n = 2000000 sum = 0 siv = sieve(n) for i in range(n): if siv[i] == i: sum += i print sum 더보기
Problem 9 - Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000. 링크 A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. python for a in range(1, 999): for b in range(a, 999): c = 1000 - (a+b) if (a*a + b*b == c*c): print a*b*c python - more efficient A Pythagorean triplet (a,b,c) has gcd(a,.. 더보기
Problem 8 - Discover the largest product of five consecutive digits in the 1000-digit number. 링크 Find the greatest product of five consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 3035890729.. 더보기
Problem 7 - Find the 10001st prime. 링크 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? python def isprime(n): if n & 1 == 0: return False for x in range(3, int(n**0.5)+1, 2): if n % x == 0: return False return True count = 1 n = 2 while count < 10001: n = n + 1 if isprime(n): count = count + 1 print n python - speed up def isprime(n): if n == 1:.. 더보기
Problem 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, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 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 nu.. 더보기
Problem 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? We think of the sequence n_p = as a number system for positive integers. For example, the prime-exponent representation of 12 is and of 18 is To multiply two numbers, we simply add their representati.. 더보기
Problem 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. python ans = 0 for i in range(100, 1000): for j in range(i, 1000): n = i * j sn = str(n) if sn == sn[::-1] and n > ans: ans = n print ans python - 1 line print max([x*y for x in range(900,1.. 더보기