일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 수학탐구
- 파이썬
- 몬테카를로
- 지오지브라
- 알지오매스
- 시뮬레이션
- 리만합
- java
- 작도
- 큰수의법칙
- 재귀함수
- 정오각형
- 피타고라스 정리
- python
- project euler
- 하합
- 큰 수의 법칙
- counting sunday
- 구분구적법
- 상합
- 프로젝트 오일러
- algeomath
- 삼각함수의그래프
Archives
- Today
- Total
이경수 선생님의 수학실험실
Problem 36(Double-base palindromes) 본문
Problem 36(Double-base palindromes)
The decimal number, \(585 = 1001001001_{2}\) (binary), is palindromic in both bases.
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
(Please note that the palindromic number, in either base, may not include leading zeros.)
In Python:
import math
import time
def ispalindrome10(n):
dig = int(math.log10(n)) + 1
nStr = str(n)
for i in range(0, int(dig / 2)):
if nStr[i] != nStr[dig - 1 - i]:
return False
return True
def ispalindrome2(n):
list = []
while(True):
list.append(n % 2)
n = n // 2
if n == 0:
break
dig = len(list)
for i in range(0, dig):
if list[i] != list[dig - 1 - i]:
return False
return True
startTime = time.time()
sumPalindrome = 0
for n in range(1, 10 ** 6):
if ispalindrome10(n) is True:
if ispalindrome2(n) is True:
sumPalindrome += n
print(sumPalindrome)
print(time.time() - startTime, "seconds")
Run Time: 1.608818769454956 seconds
In Java:
//Euler36 Double-base palindromes
package project_euler31_40;
import java.util.ArrayList;
import java.util.Collections;
public class Euler36 {
public static Boolean ispalindrome2(int n) {
ArrayList<Integer> nList = new ArrayList<Integer>();
ArrayList<Integer> rnList = new ArrayList<Integer>();
while(true) {
nList.add(Math.floorMod(n, 2));
n = Math.floorDiv(n, 2);
if (n == 0) {
break;
}
}
rnList.addAll(nList);
Collections.reverse(rnList);
for (int i = 0; i < nList.size(); i++) {
if (nList.get(i) != rnList.get(i)) {
return false;
}
}
return true;
}
public static Boolean ispalindrome10(int n) {
ArrayList<Integer> nList = new ArrayList<Integer>();
ArrayList<Integer> rnList = new ArrayList<Integer>();
while(true) {
nList.add(Math.floorMod(n, 10));
n = Math.floorDiv(n, 10);
if (n == 0) {
break;
}
}
rnList.addAll(nList);
Collections.reverse(rnList);
for (int i = 0; i < nList.size(); i++) {
if (nList.get(i) != rnList.get(i)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int sumPalindrome = 0;
for (int n = 0; n < Math.pow(10, 6); n++) {
if (ispalindrome2(n) == true && ispalindrome10(n) == true) {
sumPalindrome += n;
}
}
System.out.println(sumPalindrome);
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
}
}
Run time: 0.587seconds
Solution: 872187
'Project Euler' 카테고리의 다른 글
Problem 38(Pandigital multiples) (0) | 2019.06.15 |
---|---|
Problem 37(Truncatable primes) (0) | 2019.06.06 |
Problem 35(Circular primes) (0) | 2019.06.04 |
Problem 34(Digit factorials) (0) | 2019.06.02 |
Problem 33(Digit cancelling fractions) (0) | 2019.05.27 |
Comments