일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Geogebra
- 파이썬
- 알지오매스
- counting sunday
- 삼각함수의그래프
- 몬테카를로
- 정오각형
- 재귀함수
- 프로젝트 오일러
- java
- project euler
- 하합
- 프랙탈
- 큰 수의 법칙
- 작도
- python
- 이항분포
- 오일러
- 블록코딩
- 제곱근의뜻
- 리만합
- 구분구적법
- 상합
- 확률실험
- 피타고라스 정리
- 시뮬레이션
- 큰수의법칙
- algeomath
- 수학탐구
- 지오지브라
Archives
- Today
- Total
이경수 선생님의 수학실험실
Problem 26(Reciprocal cycles) 본문
Problem 26(Reciprocal cycles)
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
1/2= 0.5
1/3= 0.(3)
1/4= 0.25
1/5= 0.2
1/6= 0.1(6)
1/7= 0.(142857)
1/8= 0.125
1/9= 0.(1)
1/10= 0.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
In Python:
import time
def remainder(i, r):
return 10 * r % i
startTime = time.time()
sizeCycle = []
for i in range(2, 1000):
remainders = [1]
while(True):
remainders.append(remainder(i, remainders[-1]))
if remainders[-1] in remainders[0: -1] or remainders[-1] == 0:
sizeCycle.append(len(remainders))
break
print(sizeCycle.index(max(sizeCycle)) + 2)
print(time.time() - startTime, "seconds")
Run time: 0.3535029888153076 seconds
In Java:
package project_euler21_30;
import java.util.ArrayList;
import java.util.Collections;
public class Euler26 {
public static int remainder(int i, int r) {
return 10 * r % i;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
ArrayList<Integer> sizeCycle = new ArrayList<Integer>();
for (int i = 2; i < 1000; i++) {
ArrayList<Integer> remainders = new ArrayList<Integer>();
remainders.add(1);
while(true) {
int value = remainder(i, remainders.get(remainders.size() - 1));
if (remainders.contains(value) == true || value == 0) {
sizeCycle.add(remainders.size());
break;
} else {
remainders.add(value);
}
}
}
System.out.println(sizeCycle.indexOf(Collections.max(sizeCycle)) + 2);
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
}
}
Run time: 0.063seconds
Solution: 983
'Project Euler' 카테고리의 다른 글
Problem 28(Number spiral diagonals) (0) | 2019.05.12 |
---|---|
Problem 27(Quadratic primes) (0) | 2019.05.12 |
Problem 25(1000-digit Fibonacci number) (0) | 2019.05.11 |
Problem 24(Lexicographic permutations) (0) | 2019.04.22 |
Problem 23(Non-abundant sums) (0) | 2019.04.16 |
Comments