![]() |
a cute little challenge - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: a cute little challenge (/thread-184954.html) |
a cute little challenge - Don Shepherd - 05-28-2011 OK, this is similar to a challenge I offered in January, but it's a bit different. Write a program on whatever HP calculator you want to answer this question: is there a 4-digit number abcd such that aa+bb+cc+dd = abcd? Brute force is easy. How about a non-brute force?
Don
Re: a cute little challenge - Thomas Klemm - 05-28-2011 An (easy) solution for the HP-48:
\<<
Gives the solution: { 3435 }
Thanks for the challenge Re: a cute little challenge - Thomas Okken - 05-29-2011 Nice puzzle! Let's see...
None of the digits can be larger than 5, because 6^6=46656, which is not a 4-digit number.
Edited: 29 May 2011, 3:30 a.m.
Re: a cute little challenge - Thomas Klemm - 05-29-2011 What a profound analysis! Great work!
We start with a table: n : 0 1 2 3 4 5
First we check: 5, 3, 1, 0..3: 5 + 3 + 1 = 0 2 + 0 + 1 = 3
But 5,3,1,3 yields 3125+27+1+27=3,180. 5 + 4 + 3 = 3 2 + 4 + 0 = 6
This leaves the only solution: 5, 4, 3, 3
LBL "CUTE" This gives the correct answer: 3,435
Cheers
Edited: 29 May 2011, 6:34 a.m.
Re: a cute little challenge - Thomas Okken - 05-29-2011 There's no need to check permutations; you just brute-force check the 8 possible digit combinations (5310 5311 5312 5313 5430 5431 5432 5433), by calculating the sum of powers for each, and check if the result contains the same digits as the digit combination. 5433 yields the sum of powers 3435, which has the same digits as 5433, so 3435 is your answer. Edited: 29 May 2011, 3:08 p.m.
Re: a cute little challenge - Thomas Klemm - 05-29-2011 My assumption was that we have the four digits a, b, c, d and the sum s = aa+bb+cc+dd. We could calculate 10(10(10x+y)+z)+t for each permutation of a, b, c, d and compare that to s. Or we could split s into four digits A, B, C, D and check that each is contained in the set {a, b, c, d} as you suggested. For this we could use flags with the HP-42s or POS with the HP-48. My suggestion was to compare (a+b+c+d) % 9 with (A+B+C+D) % 9 = s % 9 as we wouldn't have to split s into four digits. Unfortunately we'd still have a false positive with 5 3 1 3; we could leave its falsification as an exercise to the user.
Best regards Edited: 29 May 2011, 10:34 p.m.
Re: a cute little challenge - Thomas Okken - 05-29-2011 That's a lot less efficient than doing it the other way around, though: if you enumerate all combinations of 4 digits, you have only 715 combinations to check, and if you restrict it to the digits 0...5, there are only 126. The logic for the efficient brute-force approach would be like this:
for (a = 0; a <= 9; a++) { Re: a cute little challenge - Thomas Klemm - 05-30-2011 My first attempt was the following python script:
N = range(5+1)
It took me a few minutes to type that and I got the result in 0m0.018s.
It took me probably an hour or so to translate that to the RPL program I already posted.
Nothing to be proud of. Maybe a little improved since the powers are cached.
Here's my last attempt for the HP-41: 01 LBL "CUTE" 18 RCL 11 35 RCL 15 52 RCL 14
Now also intermediate results are cached in registers 06-10 which makes the
You're using a magic function "sort_digits_ascending(s)" which I couldn't find neither
While you're correct that 126 is smaller than 625 the elapsed time depends also on what you're
And then it might also take a little more to implement your solution than my rather dumb
Kind regards
PS: Just adapted the program above for the HP-42S using RCL-arithmetic, removing line 54
Edited: 30 May 2011, 4:44 p.m.
Re: a cute little challenge - Gerson W. Barbosa - 05-30-2011 It's a bit late, but I've decided to implement my idea as of yesterday night, when Thomas & Thomas came and solved the challenge nicely. (I'd gone as far as seeing there was no digit greater than five and there was only one five, and I was also thinking of sorting the digits - that's all I'll be using). The "cuteness" of Don's problem resides in the fact that it can be solved without brute force (or at least with moderate force as in the program below) unlike, I think, "finding a 4-digit number abcd such as ab+cd = abcd".
%%HP: T(3)A(D)F(,); The solution is found in 17 seconds on the hp-50g, which is too much time when compared to the 36 seconds you have obtained on your HP-41 program. Best regards, Gerson.
Edited: 30 May 2011, 7:02 p.m.
Re: a cute little challenge - C.Ret - 05-31-2011 Quote: Dear Tomas,
In your table we read that nn=0 when n = 0. I am always surprise how a so little detail can reveal so great philosophical aspect of day to day and primary mathematic !
Is twelve (when written as 0012) a four digits number ? If the answer to this question is true, then when have to consider that 30 and 31 are also solutions of the general formulea aa+bb+cc+dd when considering 00 to be equal to 1.
Re: a cute little challenge - Thomas Klemm - 05-31-2011
Quote: There are several ways to define 00 as a limit:
Given that we look at numbers of the form nn you may consider 0 a poor choice.
Some may have noted that I cheated a little in my programs since I omitted 0. The main reason was not gain in speed I wouldn't consider twelve a four digits number. Otherwise what's gained with this expression compared to "smaller than 104"?
Thomas
Re: a cute little challenge - Gerson W. Barbosa - 05-31-2011 Seven seconds on the hp50g and there's still a lot of room for improvement as the inner loop is not so efficient as is could be. I have disregarded digits 0 because 0^0 is defined as 1 on the hp50g. BTW, it is defined differently in these two related OEIS sequences.
%%HP: T(3)A(D)F(,); Re: a cute little challenge - Thomas Klemm - 06-02-2011 Quote:Very nice! That's one of the reasons I like RPL.
Quote:
Using the '9 MOD' trick the expensive check is avoided in most of the cases: \<< { } 1 4 I had problems using { a b c } with the HP-48. The local variables are not evaluated resulting in an algebraic expression. However adding them to the list is fine. Maybe using \->LIST is faster. Other suggestions? The execution time dropped from about 1.5s to 0.6s using m48 on the iPhone.
Cheers
Edited: 2 June 2011, 6:34 a.m.
Re: a cute little challenge - Gerson W. Barbosa - 06-02-2011 Great improvement! 2.67 seconds on the hp50g. You're right, \->LIST is faster, 2.46 seconds:
%%HP: T(3)A(D)F(,); Regards,
Gerson.
Re: a cute little challenge - Paul Dale - 06-02-2011 A naive translation to the 34s of this hp-41 program takes about 0.4 seconds to find the solution in real mode and 0.3 seconds in integer mode.
001 LBL A
Re: a cute little challenge - Thomas Klemm - 06-02-2011 Quote:
Or rather? a b c 5, 4, \->LIST
Re: a cute little challenge - Thomas Klemm - 06-02-2011 Wow, that's fast. Thanks for taking the time to measure it. Just keep in mind that here we count down from 5555 to 3435. Thus bailing out at the first occurence of a solution might not be fair. Or just be careful when comparing the figures.
Kind regards Re: a cute little challenge - Gerson W. Barbosa - 06-02-2011 Oops! :-)
2.37 seconds without the unnecessesary + in the innermost loop.
Re: a cute little challenge - Andrés C. Rodríguez (Argentina) - 06-02-2011 About 0031... how could it be a solution?
Edited: 2 June 2011, 12:53 p.m.
Re: a cute little challenge - Paul Dale - 06-02-2011 Without the short cut finish it takes 0.8 seconds in real mode and 0.7 in integer mode. - Pauli
001 LBL A
Re: a cute little challenge - C.Ret - 06-03-2011 Yes, you are right, sorry for that. 31 have to be consider as a 5 digits number : [pre] 00031 = 00 + 00+00+33+11 = 1+1+1+27+1 = 31
|