From 623210b362ca77d65ecf129007f273c9b7f100d4 Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 15 Sep 2020 00:46:38 +0800 Subject: [PATCH 1/2] Created Problem04 in project_euler --- ProjectEuler/Problem04.java | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 ProjectEuler/Problem04.java diff --git a/ProjectEuler/Problem04.java b/ProjectEuler/Problem04.java new file mode 100644 index 00000000..b9d51f8b --- /dev/null +++ b/ProjectEuler/Problem04.java @@ -0,0 +1,41 @@ +package ProjectEuler; + +/** + * 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. + *

+ * link: https://projecteuler.net/problem=4 + */ +public class Problem04 { + public static void main(String[] args) { + + assert solution1(10000) == -1; + assert solution1(20000) == 19591; /* 19591 == 143*137 */ + assert solution1(30000) == 29992; /* 29992 == 184*163 */ + assert solution1(40000) == 39893; /* 39893 == 287*139 */ + assert solution1(50000) == 49894; /* 49894 == 494*101 */ + assert solution1(60000) == 59995; /* 59995 == 355*169 */ + assert solution1(70000) == 69996; /* 69996 == 614*114 */ + assert solution1(80000) == 79897; /* 79897 == 733*109 */ + assert solution1(90000) == 89798; /* 89798 == 761*118 */ + assert solution1(100000) == 99999; /* 100000 == 813*123 */ + } + + private static int solution1(int n) { + for (int i = n - 1; i >= 10000; --i) { + String strNumber = String.valueOf(i); + + /* Test if strNumber is palindrome */ + if (new StringBuilder(strNumber).reverse().toString().equals(strNumber)) { + for (int divisor = 999; divisor >= 100; --divisor) { + if (i % divisor == 0 && String.valueOf(i / divisor).length() == 3) { + return i; + } + } + } + } + return -1; /* not found */ + } +} \ No newline at end of file From c0c830546ebdd3c510aefedad4a052f28dbeaa6b Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 14 Sep 2020 16:47:16 +0000 Subject: [PATCH 2/2] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index c796ea9c..906ab463 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -203,6 +203,7 @@ ## ProjectEuler * [Problem01](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem01.java) * [Problem02](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem02.java) + * [Problem04](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem04.java) ## Searches * [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/BinarySearch.java)