The Museum of HP Calculators

HP Forum Archive 15

[ Return to Index | Top of Index ]

a simple programming challenge
Message #1 Posted by Don Shepherd on 23 May 2005, 11:59 p.m.

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
Message #2 Posted by . on 24 May 2005, 1:02 a.m.,
in response to message #1 by Don Shepherd

Playing by Valentin's rules *g* here is a program for a HP49. Execution is instant.

#include <hpgcc49.h> 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.

.

      
Re: a simple programming challenge
Message #3 Posted by Dave Shaffer on 24 May 2005, 1:32 a.m.,
in response to message #1 by Don Shepherd

There are THREE solutions if you allow the pathological case of 000!

            
Re: a simple programming challenge
Message #4 Posted by Valentin Albillo on 24 May 2005, 5:51 a.m.,
in response to message #3 by Dave Shaffer

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
Message #5 Posted by Eric Smith on 24 May 2005, 2:43 a.m.,
in response to message #1 by Don Shepherd

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.

      
Re: a simple programming challenge
Message #6 Posted by chris dean on 24 May 2005, 2:55 a.m.,
in response to message #1 by Don Shepherd

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
Message #7 Posted by Valentin Albillo on 24 May 2005, 5:16 a.m.,
in response to message #1 by Don Shepherd

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.

      
Re: a simple programming challenge
Message #8 Posted by Bram on 24 May 2005, 6:29 a.m.,
in response to message #1 by Don Shepherd

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

            
Re: a simple programming challenge
Message #9 Posted by Don Shepherd on 24 May 2005, 9:33 p.m.,
in response to message #8 by Bram

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
Message #10 Posted by EL on 25 May 2005, 3:17 a.m.,
in response to message #9 by Don Shepherd

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
Message #11 Posted by Valentin Albillo on 25 May 2005, 1:30 p.m.,
in response to message #9 by Don Shepherd

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.

                        
Giving Away HP71s
Message #12 Posted by Jake Schwartz on 31 May 2005, 10:04 a.m.,
in response to message #11 by Valentin Albillo

That "fan meeting in England" was HPCC's 15th Anniversary Conference in September of 1997....

Jake Schwartz

                              
Re: Giving Away HP71s
Message #13 Posted by Valentin Albillo on 31 May 2005, 10:18 a.m.,
in response to message #12 by Jake Schwartz

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.

                                    
Time flies
Message #14 Posted by Eric Smith on 31 May 2005, 12:59 p.m.,
in response to message #13 by Valentin Albillo

"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.

                                          
Re: Time flies
Message #15 Posted by Valentin Albillo on 31 May 2005, 1:09 p.m.,
in response to message #14 by Eric Smith

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.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall