challenge for programmable calculators
|
12-21-2013, 06:15 PM
Post: #1
|
|||
|
|||
challenge for programmable calculators
Well, it's a rainy day outside, how about a new challenge for a new forum.
Find a 3-digit number (or numbers) abc such that the sum of the digits times the product of the digits equals the original number. Or, mathematically, a 3-digit number abc such that (a+b+c) X (a X b X c) = abc. The number 000 doesn't count. All programmable calculator solutions are welcomed, including RPN, RPL, BASIC, Prime, 34s, and even non-HP programmable calculators. The brute-force solution is relatively easy. A non-brute-force solution would greatly enlighten this rainy day. |
|||
12-21-2013, 06:51 PM
Post: #2
|
|||
|
|||
RE: challenge for programmable calculators
Do you have a non-brute-force solution?
My brute force 50g program runs in about 20 seconds and gives two nontrivial solutions (in addition to the 000 solution). |
|||
12-21-2013, 07:29 PM
Post: #3
|
|||
|
|||
RE: challenge for programmable calculators
(12-21-2013 06:51 PM)kakima Wrote: Do you have a non-brute-force solution? No, I don't yet have a non-brute-force solution, I'm hoping some clever individual might come up with one. Please post your 50g code here. Thanks. |
|||
12-21-2013, 08:08 PM
Post: #4
|
|||
|
|||
RE: challenge for programmable calculators
Let me wait a few hours. I don't want my brute force solution to deter some clever individual from coming up with a better one.
Having said that, I did come up with a simple optimization that cuts the time to about 15 seconds and eliminates the false solution as well. I suppose I should also mention that this is in UserRPL. I may try SysRPL just for the fun of it. I've also got a RPN version that runs in under three seconds on an emulator. I'd like to get home and run it on real hardware before posting that one (the 50g is the only physical calculator I've got on me right now). Am I right in claiming two solutions? My programs are so simple and brute force that it's hard to imagine they're wrong, but I've been wrong before... |
|||
12-21-2013, 08:18 PM
Post: #5
|
|||
|
|||
RE: challenge for programmable calculators
[quote= A non-brute-force solution would greatly enlighten this rainy day.
[/quote] I hope the rain has gone :-) %%HP: T(3)A(D)F(.); \<< 1. 9. FOR a 1. 9. FOR b a SQ b * a b SQ * + 1. - NEG DUP SQ a b * 4. * a 100. * b 10. * + * + \v/ + a b * 2. * / DUP FP NOT NOT { DROP } { DUP 10. < { a 100. * b 10. * + + } { DROP } IFTE } IFTE NEXT NEXT \>> EVAL --> 135. 144. after 2.9 seconds (hp 50g) |
|||
12-21-2013, 08:23 PM
Post: #6
|
|||
|
|||
RE: challenge for programmable calculators
(12-21-2013 08:08 PM)kakima Wrote: Let me wait a few hours. I don't want my brute force solution to deter some clever individual from coming up with a better one. Yes, 2 solutions is correct. |
|||
12-21-2013, 10:26 PM
Post: #7
|
|||
|
|||
RE: challenge for programmable calculators
Code:
Nice use of the quadratic formula to save the inner-most loop -- compared to the brute force solution. I wonder if there's an even better way. -katie |
|||
12-21-2013, 11:26 PM
(This post was last modified: 12-21-2013 11:31 PM by Gerson W. Barbosa.)
Post: #8
|
|||
|
|||
RE: challenge for programmable calculators
(12-21-2013 10:26 PM)Katie Wasserman Wrote: Perhaps one should analyze the formula and limit the outer loop (I haven't done that). BTW, I have to say "my" method is kind of cheating, since the hard work was done by W|A: solve a*b*c*(a+b+c)==100*a+10*b+c for a, b and c |
|||
12-22-2013, 12:14 AM
Post: #9
|
|||
|
|||
RE: challenge for programmable calculators
(12-21-2013 11:26 PM)Gerson W. Barbosa Wrote:(12-21-2013 10:26 PM)Katie Wasserman Wrote: Gerson, I've never used Wolfram Alpha, but from your link, it does not appear to give the actual answer, ie, the two numbers. |
|||
12-22-2013, 12:39 AM
(This post was last modified: 12-22-2013 12:40 AM by Gerson W. Barbosa.)
Post: #10
|
|||
|
|||
RE: challenge for programmable calculators
(12-22-2013 12:14 AM)Don Shepherd Wrote: Gerson, I've never used Wolfram Alpha, but from your link, it does not appear to give the actual answer, ie, the two numbers. Don, it gives 'c' as a function of 'a' and 'b' (third formula). This saves the outer loop, so the inner loop, albeit now a bit more complicated, is executed 81 times instead of 729 times. But I think there is still room for optimization. The BASIC program might be more clear to those not familiar with RPL: Code:
|
|||
12-22-2013, 12:46 AM
Post: #11
|
|||
|
|||
RE: challenge for programmable calculators
I realize that the 17bii is not in the same league as the 50g and RPL, and is not even considered a "programmable" calculator by most (including HP), but it can solve this problem. The following solver equation stops and displays "Solution Not Found" when, ironically, it finds a solution, so you RCL ANS to see the solution. When you run it the first time, start at 111 and solve for ANS, and it finds the first solution pretty quickly. Then start at 136 and it finds the second solution quickly.
By this problem, I also wanted to discover how easy it is to use the special characters for sigma, multiply, and divide. Code:
|
|||
12-22-2013, 12:49 AM
Post: #12
|
|||
|
|||
RE: challenge for programmable calculators
Quote:I have to say "my" method is kind of cheating, since the hard work was done by W|A: I'm pretty sure that you didn't really need WA to solve that for you -katie |
|||
12-22-2013, 12:49 AM
Post: #13
|
|||
|
|||
RE: challenge for programmable calculators
(12-22-2013 12:39 AM)Gerson W. Barbosa Wrote:(12-22-2013 12:14 AM)Don Shepherd Wrote: Gerson, I've never used Wolfram Alpha, but from your link, it does not appear to give the actual answer, ie, the two numbers. Very interesting, Gerson, thanks. BASIC I understand! I'll have to study this awhile. |
|||
12-22-2013, 01:17 AM
(This post was last modified: 12-22-2013 01:36 AM by Gerson W. Barbosa.)
Post: #14
|
|||
|
|||
RE: challenge for programmable calculators
(12-22-2013 12:49 AM)Katie Wasserman Wrote: I'm pretty sure that you didn't really need WA to solve that for you Thanks! It would really have been more fun if done by hand: abc(a + b + c) = 100a + 10b + c bca^2 + acb^2 + abc^2 - 100a - 10b - c = 0 abc^2 + (ab^2 + ab^2 - 1)c - (100a + 10b) = 0 ... P.S.: Why should I be accepting programming challenges when Google reminds me it's the first day of Summer down here? So far no one from Down Under, BTW :-) |
|||
12-22-2013, 01:48 AM
(This post was last modified: 12-22-2013 01:53 AM by RMollov.)
Post: #15
|
|||
|
|||
RE: challenge for programmable calculators
(12-22-2013 01:17 AM)Gerson W. Barbosa Wrote: P.S.: Why should I be accepting programming challenges when Google reminds me it's the first day of Summer down here? So far no one from Down Under, BTW :-) Down Under we are soooo different... Haven't you noticed? Our first days of seasons are not the same as the rest of the world. |
|||
12-22-2013, 01:50 AM
Post: #16
|
|||
|
|||
RE: challenge for programmable calculators
(12-22-2013 12:46 AM)Don Shepherd Wrote:Very short code! (12-22-2013 12:46 AM)Don Shepherd Wrote: By this problem, I also wanted to discover how easy it is to use the special characters for sigma, multiply, and divide.Unfortunately one cannot start editing other people's posts anymore to see how this is done. |
|||
12-22-2013, 04:41 AM
Post: #17
|
|||
|
|||
RE: challenge for programmable calculators
Great to see the better-than-brute-force solutions.
I offer these brute force solutions as baselines for anyone else wishing to try the problem. First, UserRPL for the 50g runs in about 15 seconds: Code:
SysRPL for the 50g runs in just under two seconds: Code:
RPN for the 15C. Runs in ten seconds on the 15C+. For the 15C LE you'll have to replace the PSE with R/S then press R/S to see the other solutions. Code:
RPN for the WP 34S gives the first solution in about three seconds. R/S for the second solution almost instantaneously. Another R/S gives 0 almost instantaneously. Code:
|
|||
12-22-2013, 05:30 AM
(This post was last modified: 12-22-2013 05:57 AM by Gerson W. Barbosa.)
Post: #18
|
|||
|
|||
RE: challenge for programmable calculators
(12-22-2013 04:41 AM)kakima Wrote: RPN for the WP 34S gives the first solution in about three seconds. R/S for the second solution almost instantaneously. Another R/S gives 0 almost instantaneously. Hi, My first wp34s attempt using the built-in quadratic solver is 43 steps long (but can be reduced to 40 by eliminating three labels and using BACK and SKIP instructions instead). About 3.3 seconds to show 144, then 135 is shown instantly after R/S. Apparently no gain... |
|||
12-22-2013, 03:01 PM
(This post was last modified: 12-22-2013 03:58 PM by Thomas Klemm.)
Post: #19
|
|||
|
|||
RE: challenge for programmable calculators
(12-21-2013 06:15 PM)Don Shepherd Wrote: A non-brute-force solution would greatly enlighten this rainy day. This solution for the HP-42S reduces the amount of trials from 729 to 86 by using the symmetry of the expression. Registers: 00: a.009 01: b.009 02: c.009 03: a 04: d = 10*a 05: e = a+b 06: f = a*b 07: g = 10*(d+b) = 10*(10*a+b) Code:
Since we're lucky and the digits of the solutions are in the correct order I'm cheating a little as well and leave that part as an exercise. Cheers and thanks for the challenge Thomas PS: This Python program does the same. Code:
|
|||
12-22-2013, 03:20 PM
Post: #20
|
|||
|
|||
RE: challenge for programmable calculators
Thanks Kakima, Gerson, Katie, and Thomas. Those are some very thought-provoking solutions to this challenge.
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)