From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Palindromic Numbers Consisting of the Product of Two 3-digit Numbers

Solved!
Go to solution

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.

 

 

0 Kudos
Message 1 of 29
(4,180 Views)

My opinion is that brute-force isn't a bad idea here.  Start at 999x999 and test to see if it's a palindrome.

 

Example_VI_FP.png

 

I'm anxiously waiting for the mathemeticians to chime in.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 2 of 29
(4,143 Views)
Solution
Accepted by topic author karryli

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.). 

0 Kudos
Message 3 of 29
(4,133 Views)

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.

0 Kudos
Message 4 of 29
(4,128 Views)

Hi alten, speed do matter. This problem is partially graded on the timing, unfortunately. 

0 Kudos
Message 5 of 29
(4,127 Views)

OK, so apply my suggested improvements.  (I don't want to get graded :D)

0 Kudos
Message 6 of 29
(4,124 Views)

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. 😄

0 Kudos
Message 7 of 29
(4,110 Views)

@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.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 8 of 29
(4,063 Views)

3.4 x 10-5 to 4.8 x 10-5 version.

 

EDIT: after reading again the question I realized that the 2 numbers can be different so the result will be slower than what I measured before.

 

Ben64

0 Kudos
Message 9 of 29
(4,051 Views)

I'm in the high 30's, now...

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 10 of 29
(4,034 Views)