Last active
August 29, 2015 14:27
-
-
Save sooop/637419cfdf10e0122313 to your computer and use it in GitHub Desktop.
Project Euler on Python(3) #009 (091~100)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """정수 좌표계 상에서 두 점 P (x1, y1) 와 Q (x2, y2) 를 잡고 원점 O(0,0)와 이으면 삼각형 ΔOPQ가 만들어집니다. | |
| x, y 좌표값이 0과 2 사이일 때, 즉 0 ≤ x1, y1, x2, y2 ≤ 2 일 때 만들 수 있는 삼각형 중에서 직각삼각형은 모두 14개입니다. | |
| 0 ≤ x1, y1, x2, y2 ≤ 50 일 때는 직각삼각형이 모두 몇 개나 만들어질까요? """ | |
| """ | |
| 하나는 원점이므로 두 좌표를 가리키는 벡터를 생각하면, 벡터의 내적을 이용해서 직각 삼각형을 판별할 수 있다. 두 백터 a, b로부터 | |
| (a-b)벡터를 얻고, 이 세 벡터의 내적 중에서 하나라도 0이 있으면 직각 삼각형이다. | |
| """ | |
| from itertools import combinations | |
| limit = 50 | |
| def check(a, b): | |
| if a == b: | |
| return False | |
| x1, y1 = a | |
| x2, y2 = b | |
| x3 = x2 - x1 | |
| y3 = y2 - y1 | |
| return (x1*x2)+(y1*y2) == 0 or (x1*x3)+(y1*y3) == 0 or (x2*x3) + (y2*y3) == 0 | |
| def p91(): | |
| coords = [(x, y) for x in range(limit+1) for y in range(limit+1) if not (x == 0 and y == 0)] | |
| targets = combinations(coords, 2) | |
| c = 0 | |
| for a, b in targets: | |
| if check(a, b): | |
| c += 1 | |
| print(c) | |
| %time p91() | |
| # 14234 | |
| # Wall time: 3.22 s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 어떤 수의 각 자릿수를 제곱해서 모두 더하고, 전에 나왔던 숫자가 다시 나올때까지 같은 과정을 거듭합니다. 그러면 아래의 예와 같은 일련의 숫자들을 얻습니다. | |
| 44 → 32 → 13 → 10 → 1 → 1 | |
| 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 | |
| 위에서 알 수 있듯이 일단 1 또는 89에 도달하면 그 다음부터는 정해진 숫자들을 무한히 반복하게 됩니다. 정말 신기한 것은 어떤 숫자로 시작해도 결국에는 1이나 89에 도달한다는 사실입니다. | |
| 그러면 1천만 미만의 자연수 중에서, 이런 과정을 거쳐 89에 도달하는 수는 몇 개나 있습니까? """ | |
| """ | |
| 자리수의 제곱을 더하는 방법을 반복하여 1에 다다르는 숫자를 happy number라고 한다. (http://www.wolframalpha.com/input/?i=happy+number) | |
| http://mathworld.wolfram.com/HappyNumber.html 에서 좀 더 자세한 설명을 볼 수 있고, | |
| 1천만 이하의 happy number의 개수는 http://oeis.org/A068571 에서 찾을 수 있듯이 1418854 이다. | |
| """ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment