diff options
author | mjfernez <mjfernez@gmail.com> | 2020-02-09 15:16:26 -0500 |
---|---|---|
committer | mjfernez <mjfernez@gmail.com> | 2020-02-09 15:16:26 -0500 |
commit | 93ea7fe5957b62f18e8fbd17a21696bd7de6332d (patch) | |
tree | d90aed60d687bcf195f1150777f37cbe8a149814 /04-Largest-Palindrome-Product | |
parent | 125ec5bc3d8bfc224b7d32bcfbbc37b9fb5d441f (diff) | |
download | Project_Euler_Solutions-93ea7fe5957b62f18e8fbd17a21696bd7de6332d.tar.gz |
Organized everything, update README
Diffstat (limited to '04-Largest-Palindrome-Product')
-rwxr-xr-x | 04-Largest-Palindrome-Product/palindrome | bin | 0 -> 16824 bytes | |||
-rw-r--r-- | 04-Largest-Palindrome-Product/palindrome.c | 59 | ||||
-rw-r--r-- | 04-Largest-Palindrome-Product/palindrome.py | 36 |
3 files changed, 95 insertions, 0 deletions
diff --git a/04-Largest-Palindrome-Product/palindrome b/04-Largest-Palindrome-Product/palindrome Binary files differnew file mode 100755 index 0000000..f4d607f --- /dev/null +++ b/04-Largest-Palindrome-Product/palindrome 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 <stdlib.h> +#include <stdio.h> +#include <string.h> + +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: ") + |