BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Code Miniatures!

Let us define a "Code Miniature" as code that achieves the goal with the smallest amount of code elements.

 

(Similarly, miniature chess puzzles are defined as having a very small number of pieces, typically <7).

 

Of course speed is important too, but is it really worth throwing 5x more code at something just to gain 5% in speed? Probably not!

As it turns out, well written miniature code is typically also very efficient. 😄

 

So let's start out with the problem discussed here. (Appropriate for palindrome week! :D)

 

General Problem:

Find the largest palindromic number consisting of the product of two 3-digit numbers. A panlindromic number is defined as one whose digits are the same as its reverse. For example, 99799 is the same as its reverse "99799".

 

Miniature version of the problem (details here):

Can you solve the above problem from first principles using LabVIEW code that contains exactly the following collection of functions (plus controls/indicators, benchmark skeleton, and possibly some diagram constants):

 

 

 

Of course it would be interesting to see of somebody can do it with even less without much loss in performance. 😮

 

 

 

 

 

Message 1 of 37
(12,920 Views)

Where's the string conversion??? Smiley Surprised

 

Ill try...

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 37
(12,890 Views)

@jcarmody wrote:

Where's the string conversion??? Smiley Surprised


Don't need it. However if I use the string conversion, it slows down to about 170µs. Still pretty good, but contains a little bit more code. 😄

0 Kudos
Message 3 of 37
(12,888 Views)

I love this format.

 

I found I couldn't really use the minimalist diagram as a starting point (deductive vs inductive reasoning or some such thing...), but I came up with my own solution (which still uses the string conversion):

 

Max_Palindromic_Number_Minimalist.png 

If you know the answer is a multiple of 11, it takes ~34 us (I'm not sure this qualifies under "first principles" or not)

 

If not, it's closer to 400 us Smiley Sad

 

 

I wasn't sure how much of the performance came from just where the answer happened to fall so I went ahead and tested higher digit case (not using the multiple of 11 rule).

 

Palindromic numbers which are products of two numbers integers between 1000 and 9999 (max=99000099):

~833 us

  

 

 

Best Regards,

John Passiak
Message 4 of 37
(12,853 Views)
What's your hardware?
0 Kudos
Message 5 of 37
(12,839 Views)

@jcarmody wrote:

Where's the string conversion??? Smiley Surprised

 

Ill try...


I'm guessing he uses the quotient remainder and the for loop to do the palindrome check. That's what I did.

0 Kudos
Message 6 of 37
(12,838 Views)

@altenbach wrote:
What's your hardware?

3.1 GHz Core i5.

 

 

I hastily wrote in my initial post the 5 digit case (which I edited out once I realized the answer was wrong due to requiring 64-bit integers).

 

I re-ran the 5 digit after changing my types to u64 and got:

 

~14.2 ms

(9966006699 = 99681 * 99979)

 

 

 

Best Regards,

John Passiak
Message 7 of 37
(12,834 Views)
Assuming we know nothing about the solution or even if one exists, robust code also needs to ensure that (1) the program always actually terminates (I.e. not stuck in an infinite loop) and (2) gives a unique answer (e.g. -1) if no solution exists. Mine does.
0 Kudos
Message 8 of 37
(12,833 Views)

@altenbach wrote:
Assuming we know nothing about the solution or even if one exists, robust code also needs to ensure that (1) the program always actually terminates (I.e. not stuck in an infinite loop) and (2) gives a unique answer (e.g. -1) if no solution exists. Mine does.

Ditto...

 

Hypothetically if there were no palindromic numbers at all in the 3 digit case (no 11s rule) it would take my code ~35.3 ms to execute since it has to check all of the combinations--I tested this by changing my palindrome check to always be FALSE.  I'd bet yours is probably faster since it's not doing the string compare.

 

 

Best Regards,

John Passiak
0 Kudos
Message 9 of 37
(12,830 Views)

That was a fun little puzzle.  I ended up using the same amount of primitive functions as Altenbach did  but only ended up sharing 2/11 primitives which I thought was pretty interesting.

 

I couldn't figure out what math needed to be done to check palindromeness but I thought I ended up with a neat solution that I attached.

 

I also just realized that I forgot to add my main while loop to the list of functions as well meaning Altenbach has me by one item and John by two (maybe next time).

 

Items Used.png

Matt J | National Instruments | CLA
Message 10 of 37
(12,813 Views)