본문 바로가기

python

Problem 25 - What is the first term in the Fibonacci sequence to contain 1000 digits? 링크 The Fibonacci sequence is defined by the recurrence relation: Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term, F12, is the first term to contain three digits. What is the first term in the Fibonacci sequence to contain 1000 digits? python f1 = 1 f2 = 1 term = .. 더보기
Problem 23 - Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. 링크 A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n. As 12 is the smallest abundant number, .. 더보기
Problem 22 - What is the total of all the name scores in the file of first names? 링크 Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score. For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 .. 더보기
Problem 21 - Evaluate the sum of all amicable pairs under 10000. 링크 Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(2.. 더보기
Problem 20 - Find the sum of digits in 100! 링크 n! means n (n 1) ... 3 2 1 Find the sum of the digits in the number 100! easy with python sum([int(x) for x in str(math.factorial(100))]) 더보기
Problem 19 - How many Sundays fell on the first of the month during the twentieth century? 링크 You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible b.. 더보기
Problem 18 - Find the maximum sum travelling from the top of the triangle to the base. 링크 By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below: 75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72.. 더보기
Problem 16 - What is the sum of the digits of the number 2^1000? 링크 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? python sum([int(x) for x in str(2**1000)]) 더보기
Problem 15 - Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner? 링크 Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 2020 grid? 40 C 20 (combination) 을 구하면 된다. python pascal = [[1, 0]] for i in range(1, 41): nextrow = [1] for j in range(1, i+1): nextrow.append(pascal[i-1][j] + pascal[i-1][j-1]) nextrow.append(0) pascal.append(nextrow) print pascal[40][20] 더보기
Problem 14 - Find the longest sequence using a starting number under one million. 링크 The following iterative sequence is defined for the set of positive integers: n n/2 (n is even) n 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 40 20 10 5 16 8 4 2 1 It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting.. 더보기