일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 제곱근의뜻
- 정오각형
- 지오지브라
- python
- 구분구적법
- counting sunday
- 큰 수의 법칙
- 이항분포
- 몬테카를로
- 피타고라스 정리
- project euler
- 큰수의법칙
- 재귀함수
- 알지오매스
- 하합
- 삼각함수의그래프
- 수학탐구
- Geogebra
- 리만합
- 작도
- algeomath
- 확률실험
- 파이썬
- java
- 상합
- 시뮬레이션
- 블록코딩
- 오일러
- 프로젝트 오일러
- 프랙탈
Archives
- Today
- Total
이경수 선생님의 수학실험실
Problem 32(Pandigital products) 본문
Problem 32(Pandigital products)
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
In Python:
#PE32 Pandigital products
import time
import math
def ispandigit(n):
nList = list(str(n))
nList.sort()
for i in range(0, 9):
if i + 1 != int(nList[i]):
return False
return True
startTime = time.time()
resultList = []
for n in range(0, 2):
for i in range(10 ** n, 10 ** (n + 1)):
for j in range(10 ** (3 - n), 10 ** (4 - n)):
k = i * j
if k < math.pow(10, 4):
if ispandigit(int(str(i) + str(j) + str(k))) is True:
resultList.append(k)
print(sum(set(resultList)))
print(time.time() - startTime, "seconds")
Run time: 0.2201831340789795 seconds
In Java:
//Euler32 Pandigital products
package project_euler31_40;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collections;
public class Euler32 {
public static Boolean ispandigit(int n) {
String nStr = Integer.toString(n);
String data[];
ArrayList<Integer> nList = new ArrayList<Integer>();
data = nStr.split("");
for (int i = 0; i < nStr.length(); i++) {
nList.add(Integer.parseInt(data[i]));
}
Collections.sort(nList);
for (int i = 0; i < 9; i++) {
if (nList.get(i) != i + 1) {
return false;
}
}
return true;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int result = 0;
HashSet<Integer> resultList = new HashSet<Integer>();
for(int n = 0; n < 2; n++) {
for (int i = (int) Math.pow(10, n); i < (int) Math.pow(10, n + 1); i++) {
for (int j = (int) Math.pow(10, 3 - n); j < (int) Math.pow(10, 4 - n); j++) {
int k = i * j;
if (k < Math.pow(10, 4)) {
if (ispandigit(Integer.parseInt(
Integer.toString(i)+Integer.toString(j)+Integer.toString(k)
)) == true) {
resultList.add(k);
}
}
}
}
}
for (int n : resultList) {
result += n;
}
System.out.println(result);
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
}
}
Run time: 0.303seconds
Solution: 45228
'Project Euler' 카테고리의 다른 글
Problem 34(Digit factorials) (0) | 2019.06.02 |
---|---|
Problem 33(Digit cancelling fractions) (0) | 2019.05.27 |
Problem 31(Coin sums) (0) | 2019.05.12 |
Problem 30(Digit fifth powers) (0) | 2019.05.12 |
Problem 29(Distinct powers) (0) | 2019.05.12 |
Comments