일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 프로젝트 오일러
- 시뮬레이션
- 삼각함수의그래프
- 이항분포
- 지오지브라
- 블록코딩
- 오일러
- 큰 수의 법칙
- 리만합
- python
- 알지오매스
- project euler
- algeomath
- 확률실험
- 정오각형
- counting sunday
- 제곱근의뜻
- 재귀함수
- 파이썬
- 몬테카를로
- Geogebra
- 프랙탈
- 구분구적법
- 수학탐구
- 상합
- 작도
- 하합
- 피타고라스 정리
- 큰수의법칙
- java
- Today
- Total
이경수 선생님의 수학실험실
problem18 (Maximum path sum I) 본문
problem18 (Maximum path sum I)
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 3That 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 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
In Python:
# Maximum path sum I
import time
startTime = time.time()
f = open("./PE18_number.txt", 'r')
dataList = [int(num) for num in f.read().split()]
f.close()
maxValue = 0
for i in range(0, 16384):
product = dataList[0]
orderGroup = 1
indexSubGroup = 0
for j in range(0, 14):
if i % 2 == 0:
orderGroup += 1
indexSubGroup = indexSubGroup + 0
product += dataList[int(orderGroup * (orderGroup - 1) / 2) + indexSubGroup]
i //= 2
else:
orderGroup += 1
indexSubGroup = indexSubGroup + 1
product += dataList[int(orderGroup * (orderGroup - 1) / 2) + indexSubGroup]
i //= 2
if product > maxValue:
maxValue = product
print(maxValue)
print(time.time() - startTime, "seconds")Run time: 0.2011110782623291 seconds
In Java:
//Euler18 Maximum path sum I
package project_euler11_20;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
public class Euler18 {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
int maxValue = 0;
int orderGroup;
int indexSubGroup;
int product;
double num;
String[] data;
ArrayList<Integer> dataList = new ArrayList<Integer>();
BufferedReader br = new BufferedReader(new FileReader("./Euler18_number.txt"));
while(true) {
String line = br.readLine();
if (line == null) break;
data = line.split(" ");
for (int i = 0; i < data.length; i++) {
dataList.add(Integer.parseInt(data[i]));
}
}
br.close();
for (int i = 0; i < Math.pow(2, 14); i++) {
product = dataList.get(0);
orderGroup = 1;
indexSubGroup = 0;
num = i;
for (int j = 0; j < 14; j++) {
if (num % 2 == 0) {
orderGroup++;
product += dataList.get(orderGroup*(orderGroup - 1) / 2 + indexSubGroup);
num = Math.floor(num / 2);
}
else {
orderGroup++;
indexSubGroup++;
product += dataList.get(orderGroup*(orderGroup - 1) / 2 + indexSubGroup);
num = Math.floor(num / 2);
}
}
if (product > maxValue) {
maxValue = product;
}
}
System.out.println(maxValue);
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
}
}
Run time: 0.035seconds
solution: 1074
'Project Euler' 카테고리의 다른 글
Problem 20 (Factorial digit sum) (0) | 2019.04.07 |
---|---|
problem19 (Counting Sundays) (0) | 2019.02.16 |
problem17 (Number letter counts) (0) | 2019.02.14 |
problem16 (Power digit sum) (0) | 2019.02.14 |
Problem 15(Lattice paths) (0) | 2019.02.11 |