05-03-2015 02:57 PM
Hi, everyone. I am tring to find the maximum panlindromic number consisting of the product of two 3-digit numbers. A panlindromic number is defined as the one whose digits are the same as its reverse. For example, 99799 is the same as its reverse "99799".
I attach my vi as follows. My idea is that I find the biggest panlindromic number between 999*999 and 100*100, and then check if this number can be written as the product of two 3-digit numbers. Only when both conditions are true, the program outputs the number. If the first one which is found does not work, then we will move to the second biggest number and check if it can be written as the product of two 3-digit product, and so on...
Therefore, I use a while loop for the part of finding the biggest panlindromic number and a for loop to check the product.
Somehow, my code doesn't work, so I am thinking if anyone can give me a help.
Solved! Go to Solution.
05-03-2015 04:47 PM
My opinion is that brute-force isn't a bad idea here. Start at 999x999 and test to see if it's a palindrome.
I'm anxiously waiting for the mathemeticians to chime in.
05-03-2015 05:22 PM - edited 05-03-2015 05:25 PM
Does speed matter?
Just create a ramp array of 100...999 branch it and autoindex on two stacked FOR loops (autoindex once on the inner and once on the otuer loop). Multiply on the inner loop and test if it is a palindrome and conditionally autoindex on the inner loop if it is. Concatenate on the outer loop tunnel.
The resultin 1D array will have 2470 elements (each element in duplicate, so half that). Find the array max to get 906609. 🙂
Takes a fraction of a second. If speed matters, there are many improvements possible (e.g. we do most multiplications twice!, we only need to keep the current max in a shift register (not accumulate all elements), etc.).
05-03-2015 05:23 PM
Of course, Jim. But the thing is that this problem is partially graded on the timing. Namely, if the program takes several seconds, then I will lose some points. If there is no timing issue, I can just put two for loops here to solve the problem, and the indices will start from 1 instead of 999.
05-03-2015 05:25 PM
Hi alten, speed do matter. This problem is partially graded on the timing, unfortunately.
05-03-2015 05:26 PM
OK, so apply my suggested improvements. (I don't want to get graded :D)
05-03-2015 05:41 PM
I got a version that finds it in about 3ms on my 9 year old laptop.
I am sure there is a lot of slack left, so try to beat that as a first step, then improve from there. 😄
05-04-2015 08:01 AM
@altenbach wrote:
I got a version that finds it in about 3ms on my 9 year old laptop.
I am sure there is a lot of slack left, so try to beat that as a first step, then improve from there. 😄
Brute-force over the whole range takes me around 90 mS.
05-04-2015 08:56 AM - edited 05-04-2015 09:14 AM
05-04-2015 10:43 AM