a simple programming challenge
#1

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.

#2

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.

.

#3

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

#4

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.

#5

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.

#6

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.

#7

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.

#8

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

#9

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

#10

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

#11

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.

#12

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

Jake Schwartz

#13

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.

#14

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

#15

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.



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

Forum Jump: