a simple programming challenge Don Shepherd Posting Freak Posts: 1,392 Threads: 142 Joined: Jun 2007 05-23-2005, 11:59 PM 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. . Member Posts: 123 Threads: 7 Joined: Jan 1970 05-24-2005, 01:02 AM Playing by Valentin's rules *g* here is a program for a HP49. Execution is instant. #include int main(){ int num=0;/*number to test*/ int product, sum; int first,second,third; /*digits of a number*/ clear_screen(); for(num=1;num<1000;num++){ /*try each number between zero and a thousand*/ first=num%10; /*calculate each digit*/ second=(num/10)%10; third=(num/100)%100; product=first*second*third; /*calculate the product and sum */ sum=first+second+third; if(sum*product==num) printf("\nFound a solution: Number: %d, Product %d, Sum %d",num,product,sum); } printf("\nPress ON to quit"); while(!keyb_isON()); /* wait until ON is pressed */ } Sorry about the formatting. . Dave Shaffer (Arizona) Posting Freak Posts: 776 Threads: 25 Joined: Jun 2007 05-24-2005, 01:32 AM There are THREE solutions if you allow the pathological case of 000! Eric Smith Posting Freak Posts: 2,309 Threads: 116 Joined: Jun 2005 05-24-2005, 02:43 AM HP-41CX: first solution at 28.43 seconds from program start second solution at 38.62 seconds from program start I tried using some clever tricks, but they were slower than the brute force approach. Chris Dean Member Posts: 120 Threads: 9 Joined: Aug 2005 05-24-2005, 02:55 AM 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. Valentin Albillo Posting Freak Posts: 1,755 Threads: 112 Joined: Jan 2005 05-24-2005, 05:16 AM 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 9 20 IF A*B*C*(A+B+C)=N THEN PRINT N 30 N=N+1 @ NEXT C @ NEXT B @ NEXT A @ PRINT "OK" >RUN 135 144 OK ``` Slightly 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+B 20 FOR C=0 TO 9 @ IF P*C*(S+C)=N THEN PRINT N 30 N=N+1 @ NEXT C @ NEXT B @ NEXT A @ PRINT "OK" ``` Both 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. Valentin Albillo Posting Freak Posts: 1,755 Threads: 112 Joined: Jan 2005 05-24-2005, 05:51 AM 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. Bram Member Posts: 182 Threads: 17 Joined: Oct 2005 05-24-2005, 06:29 AM Nice challenge for the "simple" HP-12C first solution after 45 seconds; second one after 17 seconds more. end of program after well over 20 minutes ```01 0 STO 1 03 1 STO+1 0 STO 2 07 1 STO+2 0 STO 3 11 1 STO+3 RCL 1 RCL 2 + RCL 3 + RCL 1 * RCL 2 * RCL 3 * RCL 1 1 0 0 * RCL 2 1 0 * + RCL 3 + - x=0? GTO 53 39 8 RCL 3 x<=y? GTO 11 8 RCL 2 x<=y? GTO 07 8 RCL 1 x<=y? GTO 03 0 R/S 53 LASTx R/S GTO 39 ``` Don Shepherd Posting Freak Posts: 1,392 Threads: 142 Joined: Jun 2007 05-24-2005, 09:33 PM 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 EL Junior Member Posts: 28 Threads: 6 Joined: Dec 2007 05-25-2005, 03:17 AM 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 Valentin Albillo Posting Freak Posts: 1,755 Threads: 112 Joined: Jan 2005 05-25-2005, 01:30 PM 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 constructing the number by multiplying individual digits by 100 and 10. Very clever!" 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 by Hrastprogrammer is a wonderful choice. Best regards from V. Jake Schwartz Senior Member Posts: 349 Threads: 66 Joined: Apr 2007 05-31-2005, 10:04 AM That "fan meeting in England" was HPCC's 15th Anniversary Conference in September of 1997.... Jake Schwartz Valentin Albillo Posting Freak Posts: 1,755 Threads: 112 Joined: Jan 2005 05-31-2005, 10:18 AM Hi, Jake: Jake posted: "That "fan meeting in England" was HPCC's 15th Anniversary Conference in September of 1997...." "Time flies like an arrow ... ... fruit flies like a banana" (Groucho Marx) Best regards from V. Eric Smith Posting Freak Posts: 2,309 Threads: 116 Joined: Jun 2005 05-31-2005, 12:59 PM "Time flies like an arrow ..." No, you should "time flies like a scientist", by using an HP-41CX (or HP-41C/CV w/ Time Module) in stopwatch mode. Valentin Albillo Posting Freak Posts: 1,755 Threads: 112 Joined: Jan 2005 05-31-2005, 01:09 PM 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. « Next Oldest | Next Newest »

 Possibly Related Threads... Thread Author Replies Views Last Post Simple Tetris. free for you to improve on cyrille de Brébisson 3 618 11-20-2013, 05:43 PM Last Post: Erwin Ried [HP-Prime] Simple Game (Bugs) CompSystems 1 454 11-01-2013, 10:18 AM Last Post: Han HHC 2013 RPN Programming Challenge - Final Thought Jeff O. 7 856 10-17-2013, 11:02 AM Last Post: Jeff O. Simple Math Question Namir 2 463 08-09-2013, 06:13 PM Last Post: Eddie W. Shore HP-42S with Electroluminescent screen and simple I/O port Jose Poyan 8 935 03-27-2013, 07:11 PM Last Post: Jose Poyan Simple sample programs for the HP-41CX? Tom Lewis 5 606 03-25-2013, 07:11 PM Last Post: Allen Simple? programming question [WP34S] Shawn Gibson 3 500 03-15-2013, 11:56 AM Last Post: Didier Lachieze Good (simple?) calculation for benchmarking? Jedidiah Smith 28 2,306 03-01-2013, 05:13 PM Last Post: Harald HP 12 C simple interest calcuation sam 6 655 01-16-2013, 10:22 AM Last Post: fhub A simple question about the HP41C or HP41CV Antoine M. Couëtte 6 714 12-16-2012, 04:06 AM Last Post: Antoine M. Couëtte

Forum Jump: