From 20bc571dc655d0774529dd1f5517342e934809d5 Mon Sep 17 00:00:00 2001 From: mjf Date: Tue, 4 Feb 2020 14:52:21 -0500 Subject: cleanup more mistakes, add more c code --- palindrome | Bin 0 -> 16824 bytes palindrome.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ palindrome.py | 9 +-------- 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100755 palindrome create mode 100644 palindrome.c diff --git a/palindrome b/palindrome new file mode 100755 index 0000000..f4d607f Binary files /dev/null and b/palindrome differ diff --git a/palindrome.c b/palindrome.c new file mode 100644 index 0000000..aad741a --- /dev/null +++ b/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/palindrome.py b/palindrome.py index be3fe79..ed2a779 100644 --- a/palindrome.py +++ b/palindrome.py @@ -1,12 +1,9 @@ -import PIL -import math - # Problem 4 - Palindrome Products def isPalindrome(number): numchar = str(number) - middle = len(numchar)/2 + middle = int(len(numchar)/2) face = numchar[:middle] ref = numchar[len(numchar):middle-1:-1] if(face == ref): @@ -37,7 +34,3 @@ print(answer) print("The factors are: " + str(findProduct(answer))) #x = input("Type a palindromic number: ") -# if(isPalindrome(x)): -# print "The factors are: " + str(findProduct(x)) -# else: -# print "not a palindrome" -- cgit v1.2.3