일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 하합
- 알지오매스
- 파이썬
- project euler
- java
- 시뮬레이션
- 프로젝트 오일러
- 상합
- algeomath
- 큰 수의 법칙
- 이항분포
- 확률실험
- 지오지브라
- 재귀함수
- 삼각함수의그래프
- 제곱근의뜻
- 프랙탈
- 피타고라스 정리
- counting sunday
- 큰수의법칙
- 작도
- 정오각형
- 리만합
- 몬테카를로
- 오일러
- 블록코딩
- 구분구적법
- python
- Geogebra
- 수학탐구
Archives
- Today
- Total
이경수 선생님의 수학실험실
Problem 31(Coin sums) 본문
Problem 31(Coin sums)
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?
In Python:
#PE31 Coin sums
import time
startTime = time.time()
def func(change, coins, k):
if change - k * coins[0] == 0:
return 1
elif change - k * coins[0] > 0 and len(coins) == 2:
return 1
elif change - k * coins[0] < 0:
return 0
else:
return sum(func(change - k * coins[0], coins[1:], l) for l in range(0, (change - k * coins[0]) // coins[1] + 1))
coins = [200, 100, 50, 20, 10, 5, 2, 1]
print(func(200, coins, 0) + func(200, coins, 1))
print(time.time() - startTime, "seconds")
Run time: 0.12133002281188965 seconds
In Java:
package project_euler31_40;
public class Euler31 {
public static int func(int change, int coinNum, int k) {
int[] coins = {200, 100, 50, 20, 10, 5, 2, 1};
if (change - k * coins[coinNum] == 0) {
return 1;
}
else if (change - k * coins[coinNum] > 0 && coinNum == 6) {
return 1;
}
else if (change - k * coins[coinNum] < 0) {
return 0;
}
else {
int sum = 0;
int n = Math.floorDiv(change - k * coins[coinNum], coins[coinNum + 1]) + 1;
for (int i = 0; i < n; i++) {
sum += func(change - k * coins[coinNum], coinNum + 1, i);
}
return sum;
}
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
System.out.println(func(200, 0, 0) + func(200, 0, 1));
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
}
}
Run time: 0.008seconds
Solution: 73682
'Project Euler' 카테고리의 다른 글
Problem 33(Digit cancelling fractions) (0) | 2019.05.27 |
---|---|
Problem 32(Pandigital products) (0) | 2019.05.19 |
Problem 30(Digit fifth powers) (0) | 2019.05.12 |
Problem 29(Distinct powers) (0) | 2019.05.12 |
Problem 28(Number spiral diagonals) (0) | 2019.05.12 |
Comments