Post Reply 
RPL Mini-Challenge: All Odd Digits?
07-09-2017, 06:49 PM
Post: #33
RE: RPL Mini-Challenge: All Odd Digits?
(07-09-2017 03:33 PM)DavidM Wrote:  
(07-09-2017 10:49 AM)Gilles59 Wrote:  David, I like very much your approach. You can easily save 2.5 Bytes at the end with :
Code:
OVER 
instead of
DUP UNROT
In this specific case, the result will be the same

Thanks, Gilles. I'm not clear on exactly what you're suggesting, though. If I replace "DUP UNROT" with a single "OVER", the only time it will give a correct answer for all-odd-digit input is if the input only consists of 1s. As an example, 13579 is all-odd, but results in 0 under those conditions. So I must be misunderstanding what you meant.

I did ultimately decide to make a change to those two commands, though, as I think "SWAP OVER" is clearer in hindsight (though it's the same speed/size as before).

The most "expensive" part of my approach (in terms of speed) is in creating the mask. I kept thinking that there must be a better (built-in) way, and there probably is. But I gave up looking and decided to create a command (RPTCHR) that fulfills that purpose. It's similar to this program that Gerald posted, but more generic in that the repeated character is supplied as a parameter.

Though I consider it to be out-of-scope for this challenge, using RPTCHR to generate the mask made my test both smaller and faster, resulting in a score of about 2.2 (RPTCHR is wicked fast).

A quick scan of the submissions here shows that we're all approaching this using algorithms that check each individual digit for odd/even status. I keep thinking, though, that there might be some kind of mathematical approach that could be applied instead. I've looked at congruences, GCD, and a couple other experiments trying to find some combination of steps that might work here, but I'm afraid I lack the mathematical knowledge to find that kind of solution. Has anyone had any success using something other than a check-every-digit type of approach?

I'd love to be corrected, but my limited knowledge of numbers says that the parity of a digit in a number other than the final position contains no particular info concerning the whole number, ie individual digit checking is the only method.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: RPL Mini-Challenge: All Odd Digits? - Gerald H - 07-09-2017 06:49 PM



User(s) browsing this thread: 1 Guest(s)