본문 바로가기

python

Problem 35 - How many circular primes are there below one million? 링크 The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? python limit = 1000000 isPrime = [True] * limit isPrime[0] = isPrime[1] = False for i in range(2, int(round(limit**0.5))+1): .. 더보기
Problem 34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. 링크 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. python import math ans = 0 fac = [math.factorial(x) for x in range(0, 10)] for i in range(3, fac[9] * 7 + 1): if i == sum(fac[int(x)] for x in str(i)): ans += i print ans 더보기
Problem 33 - Discover all the fractions with an unorthodox cancelling method. 링크 The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s. We shall consider fractions like, 30/50 = 3/5, to be trivial examples. There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in th.. 더보기
Problem 32 - Find the sum of all numbers that can be written as pandigital products. 링크 We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital. The product 7254 is unusual, as the identity, 39 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital. Find the sum of all products whose multiplicand/multiplier/product identity can be wri.. 더보기
Problem 31 - Investigating combinations of English currency denominations. 링크 In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way: 1£1 + 150p + 220p + 15p + 12p + 31p How many different ways can £2 be made using any number of coins? python - dynamic programming items = [1, 2, 5, 10, 20, 50, 100, 200] mem = [0] * .. 더보기
Problem 30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. 링크 Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: 1634 = 14 + 64 + 34 + 44 8208 = 84 + 24 + 04 + 84 9474 = 94 + 44 + 74 + 44 As 1 = 14 is not a sum it is not included. The sum of these numbers is 1634 + 8208 + 9474 = 19316. Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. python ans = 0 for.. 더보기
Problem 29 - How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100? 링크 Consider all integer combinations of ab for 2 a 5 and 2 b 5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, 35=243 42=16, 43=64, 44=256, 45=1024 52=25, 53=125, 54=625, 55=3125 If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 How many distinct terms are in the .. 더보기
Problem 28 - What is the sum of both diagonals in a 1001 by 1001 spiral? 링크 Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 It can be verified that the sum of the numbers on the diagonals is 101. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? 손으로 풀 수도 있으나 귀찮아서 파이썬 ㄱㄱ python sum([4*x*x - 6.. 더보기
Problem 27 - Find a quadratic formula that produces the maximum number of primes for consecutive values of n. 링크 Euler published the remarkable quadratic formula: n² + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41² + 41 + 41 is clearly divisible by 41. Using computers, the incredible formula n² 79n + 1601 was discovered, which produces 80 primes fo.. 더보기
Problem 26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle. 링크 A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: 1/2 = 0.5 1/3 = 0.(3) 1/4 = 0.25 1/5 = 0.2 1/6 = 0.1(6) 1/7 = 0.(142857) 1/8 = 0.125 1/9 = 0.(1) 1/10 = 0.1 Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. Find the value of d 1000 for which.. 더보기