![]() |
a simple programming 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 simple programming challenge (/thread-73878.html) |
a simple programming challenge - Don Shepherd - 05-23-2005 OK, are you ready for a simple programming challenge? No fancy rules here, any calculator program will do. Even a voyager program will find the solution quickly. Find a 3-digit number that is equal to the product of the sum of its digits times the product of its digits. For example, 256 does not work because the sum (13) times the product (60) does not equal 256. There are two solutions, and any program will find them relatively quickly.
You can also find them on the Internet, but it's much more satisfying to find them using your own skills.
Re: a simple programming challenge - . - 05-24-2005 Playing by Valentin's rules *g* here is a program for a HP49. Execution is instant.
#include <hpgcc49.h> clear_screen();
for(num=1;num<1000;num++){ /*try each number between zero and a thousand*/
if(sum*product==num) }
printf("\nPress ON to quit"); } Sorry about the formatting.
.
Re: a simple programming challenge - Dave Shaffer (Arizona) - 05-24-2005 There are THREE solutions if you allow the pathological case of 000!
Re: a simple programming challenge - Eric Smith - 05-24-2005 HP-41CX:
I tried using some clever tricks, but they were slower than the brute force approach.
Re: a simple programming challenge - Chris Dean - 05-24-2005 Thanks for the exercise! I have used 3 loops a, b and c and tested for 100*a+10*b+c=(a+b+c)*a*b*c to get both results on my HP49G+. Entering the two results will spoil it for others.
Re: a simple programming challenge - Valentin Albillo - 05-24-2005 Hi, Don:
For the HP-71B, simplest solution is: 10 N=100 @ FOR A=1 TO 9 @ FOR B=0 TO 9 @ FOR C=0 TO 9Slightly longer but faster, as it takes some invariants out of the inner loop, is: 10 N=100 @ FOR A=1 TO 9 @ FOR B=0 TO 9 @ P=A*B @ S=A+BBoth will run on most any BASIC version out there, including SHARPs, CASIOs, etc. A straight translation of this simple code to RPN would provide an efficient solution as well, and is "left as an exercise to the reader" :-) Best regards from V.
Re: a simple programming challenge - Valentin Albillo - 05-24-2005 Sorry but 000 evaluates to 0 that is *not* a three-digit number. A three-digit number in base 10 is an integer which has exactly three digits in its decimal representation. Neither 0, nor 1, nor 27, nor 4765 fit the bill.
Best regards from V.
Re: a simple programming challenge - Bram - 05-24-2005 Nice challenge for the "simple" HP-12C
01 0
Re: a simple programming challenge - Don Shepherd - 05-24-2005 Hey, thanks guys. Very clever solutions. This is a great way to learn programming insights. Valentin, I especially liked how you initialized N to 100 and used that as your comparison variable with the sum and product, rather than constructing the number by multiplying individual digits by 100 and 10. Very clever! I really did not want to expand my calculator collection, but everytime I see Valentin use the 71B, I get a little closer to looking for one on Ebay!
Don Shepherd
Re: a simple programming challenge - EL - 05-25-2005 I concur with 135 & 144, and ran for abcd = a*b*c*d*(a+b+c+d)...no luck there, even allowing cbad, acbd, dacb... Ok, who'll be the first to write a finite element code in one sitting ;)
EL
Re: a simple programming challenge - Valentin Albillo - 05-25-2005 Hi again, Don: Don posted:
"Valentin, I especially liked how you initialized N to 100 and used that as your comparison variable with the sum and product, rather than Thanks for your appreciation, it's a well-known trick that obviates the need to reconstruct the number from its digits, which is usually a lot more expensive in RAM and running time than simply having a variable that gets incremented as each digit configuration is tried. The more digits the number has, the more the savings. "I really did not want to expand my calculator collection, but everytime I see Valentin use the 71B, I get a little closer to looking for one on Ebay!" I'm truly glad if my humble efforts serve to make HP fans aware of this wonderful, much underestimated model. Actually, HP-71Bs were extremely common a few years ago, to the point that at some fan meeting in England one was given for free to each and every attendant, including an HP-IL ROM. They had a large box with a number of HP-71Bs in there, and everyone simply grabbed one upon arriving.
Even on eBay, you can easily locate some at reasonable prices, and failing that, you can get acquainted with its awesome programming capabilities and try all my examples and challenges by using Emu71, a free emulator for Windows, or, if you've got a 48/49 and do want portability, HP-71X
Best regards from V.
Giving Away HP71s - Jake Schwartz - 05-31-2005 That "fan meeting in England" was HPCC's 15th Anniversary Conference in September of 1997.... Jake Schwartz
Re: Giving Away HP71s - Valentin Albillo - 05-31-2005 Hi, Jake: Jake posted: "That "fan meeting in England" was HPCC's 15th Anniversary Conference in September of 1997...."
... fruit flies like a banana" (Groucho Marx) Best regards from V.
Time flies - Eric Smith - 05-31-2005 "Time flies like an arrow ..."
No, you should "time flies like a scientist", by using Re: Time flies - Valentin Albillo - 05-31-2005 Hi, Eric: Eric posted: "No, you should "time flies like a scientist", by using an HP-41CX (or HP-41C/CV w/ Time Module) in stopwatch mode." Quite true. And also, I'll never understand why time flies like an arrow instead of an HP-15C, say, which is a far cuter object if not as SHARP ! :-)
Best regards from V.
|