Posts: 1,392
Threads: 142
Joined: Jun 2007
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 3digit 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.
Posts: 123
Threads: 7
Joined: Jan 1970
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.
.
Posts: 776
Threads: 25
Joined: Jun 2007
There are THREE solutions if you allow the pathological case of 000!
Posts: 2,309
Threads: 116
Joined: Jun 2005
HP41CX:
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.
Posts: 120
Threads: 9
Joined: Aug 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.
Posts: 1,755
Threads: 112
Joined: Jan 2005
Hi, Don:
For the HP71B, 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.
Posts: 1,755
Threads: 112
Joined: Jan 2005
Sorry but 000 evaluates to 0 that is *not* a threedigit number.
A threedigit 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.
Posts: 182
Threads: 17
Joined: Oct 2005
Nice challenge for the "simple" HP12C
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
Posts: 1,392
Threads: 142
Joined: Jun 2007
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
Posts: 28
Threads: 6
Joined: Dec 2007
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
Posts: 1,755
Threads: 112
Joined: Jan 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
constructing the number by multiplying individual digits by 100 and 10. Very clever!"
Thanks for your appreciation, it's a wellknown 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, HP71Bs 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 HPIL ROM. They had a large box with a number of HP71Bs 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, HP71X
by Hrastprogrammer is a wonderful choice.
Best regards from V.
Posts: 349
Threads: 66
Joined: Apr 2007
That "fan meeting in England" was HPCC's 15th Anniversary Conference in September of 1997....
Jake Schwartz
Posts: 1,755
Threads: 112
Joined: Jan 2005
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.
Posts: 2,309
Threads: 116
Joined: Jun 2005
"Time flies like an arrow ..."
No, you should "time flies like a scientist", by using
an HP41CX (or HP41C/CV w/ Time Module) in stopwatch
mode.
Posts: 1,755
Threads: 112
Joined: Jan 2005
Hi, Eric:
Eric posted:
"No, you should "time flies like a scientist", by using an HP41CX (or HP41C/CV w/ Time Module) in stopwatch mode."
Quite true. And also, I'll never understand why time flies like an arrow instead of an HP15C, say, which is a far cuter object if not as SHARP ! :)
Best regards from V.
