The Museum of HP Calculators

HP Forum Archive 19

[ Return to Index | Top of Index ]

sum of the first n integers
Message #1 Posted by Gerson W. Barbosa on 30 May 2010, 5:28 p.m.

Most everybody here knows the story of young Gauss being asked by his teacher to add together the first 100 natural numbers and how he did it quickly in a very genial way. Lacking his genius, I will do it the old-fashioned way, summing the numbers one by one, not only the first 100 of them, but as much as I can do in 60 seconds using some different programmable calculators:

HP-12C			HP-33s			HP-34c

01 1 G0001 LBL G 001 LBL A 02 ENTER G0002 1 002 1 03 ENTER G0003 ENTER 003 ENTER 04 1 H0001 LBL H 004 LBL 1 05 + H0002 1 005 1 06 + H0003 + 006 + 07 STO 0 H0004 + 007 + 08 LST x H0005 STO A 008 STO 0 09 GTO 04 H0006 LAST x 009 LST x H0007 GTO H 010 GTO 1

Execute the program and start your chronometer. When exactly one minute has elapsed, press the R/S key, recall the sum S from the memory register and compute n = (sqrt(8*S + 1) - 1)/2 or INT(e^((ln(S) + ln(8))/2 - ln(2))), in case 8*S might exceed the number of digits in the display.

The following results were obtained:

calculator S n

HP-12c+ 58 001 835 10 770

HP-33s 7 610 851 3 901

HP-12c 14 365 169

HP-34c 6 441 113

These can be used to be benchmark the performance of the calculators. For instance, I gather from the table my HP-12c+ is 63.7 times faster then the ordinary 12c (10770/169) and my 33s is 34.5 times faster than my 34c. The latter agrees with other benchmarks:

Tony Thimet's Benchmark

HP-33s: 44.0
HP-34c:  1.3

-> 33.85x

Xerxes's Benchmark

HP-34c: 4390 s 
HP-33s:  131 s     (my units)

-> 33.51x

Gene Wright's Benchmark

HP-33s: 9900   
HP-34c:  269      (my units)

-> 36.8x

Apart from the benchmarking feature, I am interested in how many counts you can get, either from trying the program in faster calculator or improving the method. For instance, on the HP-41 1 STO and then RCL 1 inside the loop is faster than the 1 in the beginning of the loop (thanks PeterP!).

Gerson.

      
Re: sum of the first n integers - HP 30b
Message #2 Posted by gene wright on 30 May 2010, 5:53 p.m.,
in response to message #1 by Gerson W. Barbosa

Ok, two timings on the HP 30b.

With TSYS OFF

S: 254,826,600 N: 22,575

With TSYS ON

S: 2,378,051,130 N: 68,964

Amazing little machine. :-)

            
Re: Buying an HP 30b
Message #3 Posted by Namir on 30 May 2010, 6:11 p.m.,
in response to message #2 by gene wright

Gene,

Finally I was able to order the HP30b online (from southern France among all places). I got two machines. Will be glad to play with the new beast!

Namir

Edited: 31 May 2010, 3:07 a.m.

            
Re: sum of the first n integers - HP 30b
Message #4 Posted by Gerson W. Barbosa on 30 May 2010, 6:19 p.m.,
in response to message #2 by gene wright

Quote:
S: 2,378,051,130 N: 68,964

That's almost 700 times more numbers than in the original problem! Well, considering the story is true which after reading the American Scientist article I am not sure about anymore. Anyway, as the Italian say, se non è vera, è ben trovata.

Quote:
Amazing little machine. :-)

Agreed! It's a shame a scientific version is still not available.

                  
Re: sum of the first n integers - HP 30b
Message #5 Posted by gene wright on 30 May 2010, 6:28 p.m.,
in response to message #4 by Gerson W. Barbosa

Well, Gerson... given its speed AND the scientific functions built-into it as well as being RPN and having a (relatively) low cost, it can certainly substitute for many scientific wishes!

I do hope a good number here will get one as they can. And, no, I get no commission. :-)

                        
Re: sum of the first n integers - HP 30b
Message #6 Posted by Gerson W. Barbosa on 30 May 2010, 7:41 p.m.,
in response to message #5 by gene wright

I read your review of the HP 30b in Datafile V28 N6 and found it impressive. I would get myself one, but I haven't seen it around yet. Sure I don't like inverse trigs accessible through menu, as I didn't like it on the 28S, but at least they are there.

                              
Re: sum of the first n integers - HP 30b
Message #7 Posted by gene wright on 31 May 2010, 12:17 a.m.,
in response to message #6 by Gerson W. Barbosa

Yes, the inverse trig are there and can be assigned to the keyboard at the cost of about 24 of the 290 bytes and 3 of the 10 program locations. Really not too bad to make "run" mode much more useful. Make sure to get a copy of the HP Solve newsletter now being edited by Richard Nelson. Has had a couple of articles about the 30b with perhaps more to come.

      
Re: sum of the first n integers
Message #8 Posted by Scott Newell on 30 May 2010, 6:58 p.m.,
in response to message #1 by Gerson W. Barbosa

12C+, custom integer firmware, overclocked to 48 MHz. N=152,361,930 and S=11,607,078,932,843,415. (Had to use 64 bits for the accumulator on the 12C+, and I had to check my results with bc to handle the range.)

   unsigned long long acc=0;
   do
   {
      ++x;
      acc+=x;
   } while ((((*(unsigned long int*)PIOC_PDSR) & c_mask)==c_mask));
            
Re: sum of the first n integers
Message #9 Posted by Gerson W. Barbosa on 30 May 2010, 8:09 p.m.,
in response to message #8 by Scott Newell

Quote:
N=152,361,930 and S=11,607,078,932,843,415.

That's really amazing, a hard-to-break record on the 12C+! My hopes now lie on the 50g overclocked to maximum speed. Anyone? :-)

I checked your value for N on the 50g in exact mode. It worked but the calculator froze afterwards, probably because the Ni-MH had gotten low. As it would not turn off I removed one of the batteries for a moment and it restarted with the memory intact.

                  
Re: sum of the first n integers
Message #10 Posted by Scott Newell on 30 May 2010, 8:31 p.m.,
in response to message #9 by Gerson W. Barbosa

Quote:
That's really amazing, a hard-to-break record on the 12C+! My hopes now lie on the 50g overclocked to maximum speed. Anyone? :-)

I suspect a C or assembly coded routine on either the 50G or the Casio would easily beat it. I can't seem to get the 12C+ to run reliably at 50MHz, and those calcs should do what, 200?

                        
Re: sum of the first n integers
Message #11 Posted by Tim Wessman on 30 May 2010, 10:14 p.m.,
in response to message #10 by Scott Newell

50? We keep it under 36 always as there is a risk of burning it out. . . sheesh!

TW

                        
Re: sum of the first n integers
Message #12 Posted by Eric Smith on 1 June 2010, 7:16 p.m.,
in response to message #10 by Scott Newell

200 MHz? Where did you get that? The part has a rated maximum of 36 MHz. If you exceed that by much, reads of the flash memory will become unreliable.

                              
Re: sum of the first n integers
Message #13 Posted by Gerson W. Barbosa on 1 June 2010, 7:28 p.m.,
in response to message #12 by Eric Smith

I think he's referring to the 50g's processor, which is underclocked at 75MHz but can run at higher speeds, up to 192MHz according to Xerxes's Benchmark article.

Edited: 1 June 2010, 7:32 p.m.

                              
Re: sum of the first n integers
Message #14 Posted by Scott Newell on 1 June 2010, 10:10 p.m.,
in response to message #12 by Eric Smith

Quote:
200 MHz? Where did you get that? The part has a rated maximum of 36 MHz. If you exceed that by much, reads of the flash memory will become unreliable.
I looked up the 50G and the Casio on Wikipedia, and they claimed much faster CPUs (50G: 203 MHz?, Casio: 133 MHz?), with cache.

I have not bothered to run code from sram, but that might make it to 50MHz or so. I have also not tried to increase the number of flash wait states.

I did look through the AT91SAM7L128 datasheet (mine may not be the latest version) abs max ratings, but couldn't find a hard limit of 36 MHz. The PLL shows a max of 47 MHz, master clock max of 39.7 MHz, and max flash operating frequency of 39.7 MHz. Considering how sloppy the PLL reference is (20-44 kHz!), I just assumed I could overclock it a bit, just for fun.

      
Re: sum of the first n integers in HP 35s
Message #15 Posted by Pablo P (Spain) on 31 May 2010, 4:41 a.m.,
in response to message #1 by Gerson W. Barbosa

HP 35s
----------
S: 480 690
N: 980
----------
A001 LBL A
A002 1
A003 ENTER
A004 ENTER
A005 1
A006 +
A007 +
A008 STO A
A009 LASTx
A010 GTO A005

It is very slow compared to HP 33s.

Edited: 31 May 2010, 4:45 a.m.

            
Re: sum of the first n integers in HP 35s
Message #16 Posted by Gerson W. Barbosa on 31 May 2010, 6:11 p.m.,
in response to message #15 by Pablo P (Spain)

HP-33s

G0001 LBL G G0002 1 G0003 ENTER G0004 STO B H0001 LBL H H0002 RCL+ B H0003 + H0004 STO A H0005 LAST x H0006 GTO H

S: 11 122 686 n: 4 716

Is the extra ENTER really necessary on the 35s? On the 33s, it is not.

One less instruction inside the loop causes the count to increase about 20% (6/5), but the gain should be even greater on the 35s (Courtesy of Katie Wasserman :-)

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv017.cgi?read=119195

                  
Re: sum of the first n integers in HP 35s
Message #17 Posted by Pablo P (Spain) on 31 May 2010, 7:16 p.m.,
in response to message #16 by Gerson W. Barbosa

Hi Gerson, the second ENTER was unnecessary. Thanks

The improved program without the constant in the loop:

HP 35s
----------
S: 1 813 560
N:     1 904
----------
A001 LBL A
A002 1
A003 STO B
A004 ENTER
A005 RCL+ B
A006 +
A007 STO A
A008 LASTx
A009 GTO A005

Edited: 31 May 2010, 7:16 p.m.

      
A faster program for the HP33s
Message #18 Posted by Palmer O. Hanson, Jr. on 31 May 2010, 11:23 p.m.,
in response to message #1 by Gerson W. Barbosa

Gerson:

Try this on the HP-33s starting with LASTx cleared by entering 0 X:

LBL A

LASTx

1

+

+

GTO A

Read the sum from the display, rolling down if necessary. Read n by pressing LASTx, or as one less than the number in the display if the content of LASTx is 1.

To cross check calculate n(n+1)/2.

Two one minute tries with this method yielded

Sum = 9,114,315 with n + 4,269

Sum + 9,097,245 with n + 4,265

Palmer

            
Re: A faster program for the HP33s
Message #19 Posted by Gerson W. Barbosa on 1 June 2010, 7:19 p.m.,
in response to message #18 by Palmer O. Hanson, Jr.

Hello Palmer,

Quote:
Try this on the HP-33s starting with LASTx cleared by entering 0 X:

...

Read the sum from the display, rolling down if necessary. Read n by pressing LASTx, or as one less than the number in the display if the content of LASTx is 1.

To cross check calculate n(n+1)/2.


You're right, I should have sticked to the old K.I.S.S. rule :-)

Your HP-33s is a bit faster than mine. I obtained 4245 and 4254.

The slightly modified version below is even faster, provided 1 is previously stored in register B:

A0001 LBL A		
A0002 LASTx			
A0003 RCL+ B
A0004 +		
A0005 GTO A

time (s) n S

59 + 99/100 5 255 13 810 140

59 + 97/100 5 252 13 794 378

Thanks for your suggestion,

Gerson.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall