이경수 선생님의 수학실험실

Problem 36(Double-base palindromes) 본문

Project Euler

Problem 36(Double-base palindromes)

(이경수) 2019. 6. 6. 16:09

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