From 10d8dc486580f950b06fb632913822b3fc77c2e8 Mon Sep 17 00:00:00 2001 From: daniele Date: Wed, 9 Aug 2023 17:34:57 +0200 Subject: [PATCH 1/5] Update .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index c2ae10d..afe8e35 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,5 @@ /out/production/Stack/stack/Stack.class /Stack/Stack.iml /out/production/Stack/stack/StackTest.class +/LychrelNumbers/LychrelNumbers.iml +/out/production/LychrelNumbers/lychrel/LychrelTest.class From 73e891844548029ee2b659953c8c302d2d51cde4 Mon Sep 17 00:00:00 2001 From: daniele Date: Wed, 9 Aug 2023 17:35:25 +0200 Subject: [PATCH 2/5] First test for checking if runtime env is ok --- LychrelNumbers/src/lychrel/LychrelTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 LychrelNumbers/src/lychrel/LychrelTest.java diff --git a/LychrelNumbers/src/lychrel/LychrelTest.java b/LychrelNumbers/src/lychrel/LychrelTest.java new file mode 100644 index 0000000..8216478 --- /dev/null +++ b/LychrelNumbers/src/lychrel/LychrelTest.java @@ -0,0 +1,10 @@ +package lychrel; + +import org.junit.Test; + +public class LychrelTest { + @Test + public void nothing(){ + + } +} From 48cf883a24ae4b72eedb6700fb499223f6befd28 Mon Sep 17 00:00:00 2001 From: daniele Date: Thu, 10 Aug 2023 15:55:27 +0200 Subject: [PATCH 3/5] Consider all numbers < 10 as palindromes --- LychrelNumbers/src/lychrel/Lychrel.java | 14 ++++++++++++++ LychrelNumbers/src/lychrel/LychrelTest.java | 14 +++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 LychrelNumbers/src/lychrel/Lychrel.java diff --git a/LychrelNumbers/src/lychrel/Lychrel.java b/LychrelNumbers/src/lychrel/Lychrel.java new file mode 100644 index 0000000..d0dc33e --- /dev/null +++ b/LychrelNumbers/src/lychrel/Lychrel.java @@ -0,0 +1,14 @@ +package lychrel; + +public class Lychrel { + public static int convergesAtIteration(int number, int limit) { + if (isPalindrome(number)) return 0; + else return 1; + } + + private static boolean isPalindrome(int number) { + if(number <= 9) return true; + + return false; + } +} diff --git a/LychrelNumbers/src/lychrel/LychrelTest.java b/LychrelNumbers/src/lychrel/LychrelTest.java index 8216478..6076be2 100644 --- a/LychrelNumbers/src/lychrel/LychrelTest.java +++ b/LychrelNumbers/src/lychrel/LychrelTest.java @@ -2,9 +2,21 @@ import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class LychrelTest { + + private final int LIMIT = 1000; + @Test - public void nothing(){ + public void facts(){ + convergesAtIteration(1, 0); + convergesAtIteration(2, 0); + convergesAtIteration(10, 1); + convergesAtIteration(11, 0); + } + private void convergesAtIteration(int number, int iteration) { + assertEquals(iteration, Lychrel.convergesAtIteration(number, LIMIT)); } } From 6dfd6f2029cbc8479af6e0c61727ff5a739ce5da Mon Sep 17 00:00:00 2001 From: daniele Date: Thu, 10 Aug 2023 17:08:18 +0200 Subject: [PATCH 4/5] Final implementation --- LychrelNumbers/src/lychrel/Lychrel.java | 34 ++++++++++++-- LychrelNumbers/src/lychrel/LychrelTest.java | 51 ++++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/LychrelNumbers/src/lychrel/Lychrel.java b/LychrelNumbers/src/lychrel/Lychrel.java index d0dc33e..7e00803 100644 --- a/LychrelNumbers/src/lychrel/Lychrel.java +++ b/LychrelNumbers/src/lychrel/Lychrel.java @@ -1,14 +1,38 @@ package lychrel; +import java.math.BigInteger; + public class Lychrel { public static int convergesAtIteration(int number, int limit) { - if (isPalindrome(number)) return 0; - else return 1; + return converge(BigInteger.valueOf(number), 0, limit); + } + + private static int converge(BigInteger number, int iteration, int limit) { + if (!isPalindrome(number) && iteration < limit) + return converge(number.add(reverse(number)), iteration + 1, limit); + else + return iteration; + } + + static BigInteger reverse(BigInteger number) { + char[] nDigits = number.toString().toCharArray(); + char[] rDigits = new char[nDigits.length]; + int lastIndex = nDigits.length - 1; + + for (int idx = 0; idx < nDigits.length; idx++) + rDigits[idx] = nDigits[lastIndex - idx]; + + return new BigInteger(new String(rDigits)); } - private static boolean isPalindrome(int number) { - if(number <= 9) return true; + static boolean isPalindrome(BigInteger number) { + String digits = number.toString(); + int lastIndex = digits.length() - 1; + + for (int idx = 0; idx < digits.length(); idx++) + if (digits.charAt(idx) != digits.charAt(lastIndex - idx)) + return false; - return false; + return true; } } diff --git a/LychrelNumbers/src/lychrel/LychrelTest.java b/LychrelNumbers/src/lychrel/LychrelTest.java index 6076be2..eab2d90 100644 --- a/LychrelNumbers/src/lychrel/LychrelTest.java +++ b/LychrelNumbers/src/lychrel/LychrelTest.java @@ -1,8 +1,11 @@ package lychrel; +import org.junit.Ignore; import org.junit.Test; -import static org.junit.Assert.assertEquals; +import java.math.BigInteger; + +import static org.junit.Assert.*; public class LychrelTest { @@ -14,9 +17,55 @@ public void facts(){ convergesAtIteration(2, 0); convergesAtIteration(10, 1); convergesAtIteration(11, 0); + convergesAtIteration(19, 2); + convergesAtIteration(78, 4); + convergesAtIteration(89, 24); + + doesNotConverge(196); + } + + private void doesNotConverge(int number) { + convergesAtIteration(number, LIMIT); } private void convergesAtIteration(int number, int iteration) { assertEquals(iteration, Lychrel.convergesAtIteration(number, LIMIT)); } + + @Test + public void palindromes(){ + isPalindrome(1); + isPalindrome(11); + isPalindrome(121); + isPalindrome(12321); + isPalindrome(1234321); + } + + @Test + public void notPalindromes(){ + isNotPalindrome(10); + isNotPalindrome(12331); + isNotPalindrome(124321); + } + + private void isNotPalindrome(int number) { + assertFalse(Lychrel.isPalindrome(BigInteger.valueOf(number))); + } + + private void isPalindrome(int number){ + assertTrue(Lychrel.isPalindrome(BigInteger.valueOf(number))); + } + + @Test + public void reversals(){ + reversed(1,1); + reversed(12, 21); + reversed(13, 31); + reversed(123, 321); + reversed(1234, 4321); + } + + private void reversed(int n, int r) { + assertEquals(BigInteger.valueOf(r), Lychrel.reverse(BigInteger.valueOf(n))); + } } From b4b451cefb66bc25d92ca28849c3c2fd10cdb6b3 Mon Sep 17 00:00:00 2001 From: daniele Date: Sun, 13 Aug 2023 15:53:36 +0200 Subject: [PATCH 5/5] Update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index afe8e35..b5f1340 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,6 @@ /out/production/Stack/stack/StackTest.class /LychrelNumbers/LychrelNumbers.iml /out/production/LychrelNumbers/lychrel/LychrelTest.class +/.idea/sonarlint/issuestore/7/1/718d8bc80461d3aa9995862c1883b60a6574e27c +/.idea/sonarlint/securityhotspotstore/7/1/718d8bc80461d3aa9995862c1883b60a6574e27c +/out/production/LychrelNumbers/lychrel/Lychrel.class