일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- Crank-Nicolson
- 우선순위큐
- heap
- Fluid Dynamics
- Fluids
- projecteuler
- 통계학
- Blasius
- 회귀
- 유체역학
- Finite Difference Method
- Python
- Heat Equation
- python3
- 힙
- Statistics
- 디스크 컨트롤러
- 예제
- 이중우선순위큐
- regression
- 프로젝트오일러
- Turbulent
- 파이썬
- programmers
- 프로그래머스
- Boundary Layers
- FTCS
- Navier-Stokes
- Compressible Flow
- Laminar
- Today
- Total
Sudal's Garage
Project Euler 42 본문
Question:
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
문제:
삼각수는 다음과 같은 수열로 정의된다.tn = ½n(n+1). 이 수열의 첫 번째 항부터 열 번째 항까지 나타내면 다음과 같다.
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
반면에 어떠한 영어 단어가 주어졌을 때, 각 알파벳들의 알파벳 순서 값들을 더한 것을 그 단어의 가치라고 정의해보자. 예를 들어 영어 단어 SKY의 가치는 19 + 11 + 25 = 55 = t10이고, 이것은 삼각수에 해당한다. 만약 어떤 단어의 가치에 해당하는 값이 삼각수일 때 그것을 삼각단어라고 부르자.
다음 파일은 16K 텍스트 파일로 거의 2000개의 일반적으로 사용되는 영어 단어가 포함되어 있다.(words.txt) 이 영어단어들 중 삼각단어는 총 몇 개일까?
Solution:
from math import sqrt import time f = open('p042_words.txt','r') data = f.read() # data 에 파일 정보를 저장한다. f.close() data = data.replace('"',"").replace("'","").split(',') def isTri(n): x = (sqrt(8*n + 1) - 1) / 2 return x == int(x) def alpNum(alphabet): return ord(alphabet) - 64 tri = 0 start = time.time() for i in range(len(data)): summ = 0 for j in range(len(data[i])): summ += alpNum(data[i][j]) if isTri(summ): tri += 1 print("""The number of triangular number: {0} Found in ...{1:.5f}s""".format(tri,time.time() - start))
삼각수 판별은 다음과 같이 할 수 있다. 출처: https://en.wikipedia.org/wiki/Triangular_number
0.005 초 걸렸다!
'Programming > Project Euler - python' 카테고리의 다른 글
Project Euler 44 (0) | 2019.04.06 |
---|---|
Project Euler 43 (0) | 2019.04.05 |
Project Euler 41 (0) | 2019.04.03 |
Project Euler 40 (0) | 2019.04.02 |
Project Euler 39 (0) | 2019.04.01 |