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

Problem 4(Largest palindrome product) 본문

Project Euler

Problem 4(Largest palindrome product)

(이경수) 2019. 2. 7. 16:28

Problem 4(Largest palindrome product)

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.



In Python:

product=0; result=0; num=[]

for i in range(100, 1000):
for j in range(100, 1000):
product=i*j
num=str(product)

if num==num[::-1]:
if product>result:
result=product

print(result)

Run time: 1.6162359714508057 seconds

In Java:

package project_euler;


import java.util.ArrayList;


public class Euler4 {

public static void main(String[] args) {

long startTime = System.currentTimeMillis();

ArrayList<Integer> numList1 = new ArrayList<Integer>();

ArrayList<Integer> numList2 = new ArrayList<Integer>();

int product = 0;

int result = 0;

for (int i = 100; i < 1000; i++) {

for (int j = 100; j < 1000; j++) {

product = i * j;

while (product > 0) {

numList1.add(product % 10);

numList2.add(0, product % 10);

product /= 10;

}

if (numList1.equals(numList2)) {

if (i * j > result) {

result = i * j;

}

}

numList1.clear();

numList2.clear();

}

}

System.out.println(result);

long endTime = System.currentTimeMillis();

System.out.println((double)(endTime - startTime)/(double)1000 +"seconds");

System.out.println((endTime - startTime) +"ms");

}

}

Run time: 1.022seconds, 1022ms


solution: 906609


[from Project Euler: https://projecteuler.net/problem=4]



'Project Euler' 카테고리의 다른 글

Problem 6(Sum square difference)  (0) 2019.02.08
Problem 5(Smallest multiple)  (0) 2019.02.08
Problem 3(Largest prime factor)  (0) 2019.02.06
Problem 2(Even Fibonacci numbers)  (0) 2019.02.06
Problem 1(Multiples of 3 and 5)  (0) 2019.02.06
Comments