From 93ea7fe5957b62f18e8fbd17a21696bd7de6332d Mon Sep 17 00:00:00 2001 From: mjfernez Date: Sun, 9 Feb 2020 15:16:26 -0500 Subject: Organized everything, update README --- 04-Largest-Palindrome-Product/palindrome | Bin 0 -> 16824 bytes 04-Largest-Palindrome-Product/palindrome.c | 59 ++++++++++++++++++++++++++++ 04-Largest-Palindrome-Product/palindrome.py | 36 +++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100755 04-Largest-Palindrome-Product/palindrome create mode 100644 04-Largest-Palindrome-Product/palindrome.c create mode 100644 04-Largest-Palindrome-Product/palindrome.py (limited to '04-Largest-Palindrome-Product') diff --git a/04-Largest-Palindrome-Product/palindrome b/04-Largest-Palindrome-Product/palindrome new file mode 100755 index 0000000..f4d607f Binary files /dev/null and b/04-Largest-Palindrome-Product/palindrome differ diff --git a/04-Largest-Palindrome-Product/palindrome.c b/04-Largest-Palindrome-Product/palindrome.c new file mode 100644 index 0000000..aad741a --- /dev/null +++ b/04-Largest-Palindrome-Product/palindrome.c @@ -0,0 +1,59 @@ +#include +#include +#include + +int isPalindrome(long num){ + long len = 0; + long n = num; + while(n != 0){ + n = n / 10; + len++; + } + char *numchar = (char *) malloc (len * sizeof(char)); + sprintf(numchar, "%ld", num); + //printf("%s\n", numchar); + // Backwards counter + long j = len - 1; + // Forwards counter + long i = 0; + + while(i < len){ + if(numchar[i] != numchar[j]) + return 0; + i++; + j--; + } + return 1; +} + +long *findProduct(long num){ + for(long i = 999; i > 100; i--) + for(long j = 999; j > 100; j--) + if(i * j == num){ + long *facts = (long *) malloc (2 * sizeof(long)); + facts[0] = i; + facts[1] = j; + return facts; + } +} + +long findMaxPalindrome(){ + long large = 0; + for(long i = 999; i > 100; i--){ + for(long j = 999; j > 100; j--){ + long test = i * j; + if(isPalindrome(test) && test > large){ + large = test; + } + } + } + return large; +} + +int main(){ + long answer = findMaxPalindrome(); + printf("%d\n", answer); + long *prod = findProduct(answer); + printf("The factors are %d and %d\n", prod[0], prod[1]); +} + diff --git a/04-Largest-Palindrome-Product/palindrome.py b/04-Largest-Palindrome-Product/palindrome.py new file mode 100644 index 0000000..ed2a779 --- /dev/null +++ b/04-Largest-Palindrome-Product/palindrome.py @@ -0,0 +1,36 @@ +# Problem 4 - Palindrome Products + + +def isPalindrome(number): + numchar = str(number) + middle = int(len(numchar)/2) + face = numchar[:middle] + ref = numchar[len(numchar):middle-1:-1] + if(face == ref): + return True + else: + return False + + +def findProduct(number): + for i in range(999, 100, -1): + for j in range(999, 100, -1): + if(i*j == number): + return [i, j] + + +def findMaxPalindrome(): + large = 0 + for i in range(999, 100, -1): + for j in range(999, 100, -1): + test = i*j + if(isPalindrome(test) and test > large): + large = test + return large + + +answer = findMaxPalindrome() +print(answer) +print("The factors are: " + str(findProduct(answer))) +#x = input("Type a palindromic number: ") + -- cgit v1.2.3