본문 바로가기

Algorithm

(5)
[Euler Project] Problem 25 using Python Euler Project 25번 문제는 피보나치 수열 문제이다. 꽤나 큰 수의 피보나치 수(1000자리)가 등장해야 문제가 종료되고, 전체 피보나치 수열이 필요 없으므로, 필요한 정보만 관찰한다. 관찰할 정보는 관찰중인 피보나치 수열의 마지막 두 수와 전체 피보나치 수열의 길이이다. """ The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, 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 Th..
[Euler Project] Problem 24 using Python Euler Project 24번 문제는 permutation이라는 logic 자체가 작은 문제들로 나눠서 풀 수 있는 문제다. 따라서 제귀적으로 앞 자리부터 하나의 digit씩 찾아가면 문제의 답을 찾을 수 있다. """ A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 a..
[Euler Project] Problem 23 using Python 특정 숫자의 약수의 합이 해당 숫자와 같으면 perfect number, 해당 숫자보다 작으면 deficient, 크면 abundant number이다. 28123 이상의 수는 두 abundant numbers의 합으로 나타낼 수 있다는 것을 받아들이고 시작하며, 두 abundant number의 합으로 나타낼 수 없는 수의 총합을 구하는 문제이다. 빠른 방법을 찾을 수도 있을 것 같은데, 처음 짠 straightforward한 코드도 금방 값을 구해낼 수 있어서 더 고민 않고 넘어가기로 한다. """ A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example,..
[Euler Project] Problem 22 using Python Euler Project 22번 문제는 5000개 이상의 이름들이 나열된 파일이 제공된다. 요구되는 값은 이름을 정렬한 후, 정렬 순위와 이름 값을 곱한 값을 모두 더한 것이다. 이름 값이란, 아래의 get_alphabet_score로 구현되어 있으며, a, b, c ... 순으로 1, 2, 3 점을 누적 부여한다. 예를 들어 COLIN = 3 + 15 + 12 + 9 + 14 = 53이다. 이후는 순차적으로 진행될 뿐 큰 문제가 없다. """ 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 alp..
[Euler Project] Problem 21 using Python Euler Project 21번 문제는 10000 이하의 Amicable numbers를 구하는 문제이다. 특정 수 a 의 약수의 합을 b라고 하고, 해당 관계를 d(a) = b 로 나타낼 때, d(a) = b and d(b) = a 일 경우 a와 b는 amicable numbers다. 단순히 모든 경우를 계산해도 수 초 내에 계산이 가능하다. """ Euler Project 21: Amicable numbers 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 a..