Dismal Arithmetic & 3 Progs for 49G & Others
05-11-2015, 04:16 PM
Post: #21
 Dave Britten Senior Member Posts: 2,168 Joined: Dec 2013
RE: Dismal Arithmetic & 2 Progs for 49G, the Prime, 48S & 32S & TI & C...
Now for something in BASIC: the Casio fx-702p "programmable calculator" (really more of a pocket computer). This is the slowest one yet, at 1:44 to do 435,821 * 2,949,229, but it DOES yield the full-precision result, 244,585,835,821, unlike the Casio graphing calculator. It displays 2.445858358E11, but you can pry the other digits out by executing F-2.445E11.

Just run it, tell it if you want to "A"dd, or "M"ultiply, then enter your two inputs.

Code:
1 INP "(A)DD (M)ULT:",$2$=MID(1,1) 10 IF $="A":INP "X",A,"Y",B:GSB 300:PRT S 20 IF$="M";INP "X",X,"Y",Y:GSB 400:PRT F 30 END 100 IF N=0;L=0:RET 110 L=INT LOG N+1:RET 200 R=FRAC (INT (N/10^(D-1))/10)*10:RET 300 N=A:GSB 100:C=L 310 N=B:GSB 100:IF L>C;C=L 320 S=0 330 FOR I=1 TO C 340 N=A:D=I:GSB 200:T=R 350 N=B:GSB 200:IF R>T;T=R 360 S=S+T*10^(I-1) 370 NEXT I 380 RET 400 N=X:GSB 100:Z=L 410 N=Y:GSB 100:J=L 420 F=0 430 FOR J=J TO 1 STEP -1 440 N=Y:D=J:GSB 200:M=R 450 P=0 460 FOR K=1 TO Z 470 U=M 480 N=X:D=K:GSB 200 490 IF R<U;U=R 500 P=P+U*10^(K-1) 510 NEXT K 520 A=P:B=10*F:GSB 300:F=S 530 NEXT J 540 PRT F Routines 1 Interactive Menu 100 Number Length 200 Get Digit 300 Add 400 Multiply Variables N - Number Length input L - Number Length result N - Get Digit input D - Get Digit digit number R - Get Digit result A - Addend 1 B - Addend 2 C - Add loop limit (addend max length) I - Add loop counter S - Sum T - Addition scratch register X - Factor 1 Y - Factor 2 Z - Factor 1 length J - Factor 2 length/outer loop counter K - Inner loop counter (factor 1) P - Product F - Sum of products M - Factor 2 current digit U - Multiply scratch register (digit comparison)

Notes: I think this is one of Casio's first BASIC machines, so it's got a few odd limitations.

A number of keywords are abbreviated from what's typically used (PRT, INP, GSB, RET, etc).

Variable naming and availability are limited much like the Sharp pocket computers. Single-character names, and some basic array functionality. Variables can hold numbers or strings (e.g. A vs. A$), but are limited to 7-character strings. The dedicated string variable, named simply$, can hold 30 characters.

There are two forms of the IF statement. This will do a GOTO if true:
IF X>3 THEN 90
And this will skip the rest of the line if false:
IF X>3;F=7:GSB 300
Note that you have to terminate the IF with a semicolon, whereas you use a colon to separate the rest of the statements.

The IF statement can't evaluate any compound expressions; there's no AND or OR functions! But you can string conditions together into a simple AND chain like this:
IF X>3;IF J<10;GSB 500:RET
Anything more complicated, and you're going to be doing GOTO stunts.

There's no DATA or READ statements. Not an issue here, but it was an annoying discovery when I was porting the usual prime factors program.

It's sloooow. It took almost 5 times as long as running the same algorithm on the Casio fx-8500g.

Despite all that, I like it, largely because it has slots for 10 independent programs, like most Casios do. The Sharp and TI offerings typically only provide a single program space.
05-11-2015, 06:04 PM
Post: #22
 Marcus von Cube Senior Member Posts: 760 Joined: Dec 2013
RE: Dismal Arithmetic & 2 Progs for 49G, the Prime, 48S & 32S & TI & C...
For additional info about pocket computer BASIC dialects I recommend looking at my BASIC Comparison Sheet.

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
05-13-2015, 09:19 AM (This post was last modified: 09-08-2022 06:16 AM by Gerald H.)
Post: #23
 Gerald H Senior Member Posts: 1,567 Joined: May 2014
RE: Dismal Arithmetic & 3 Progs for 49G & Others
Given DPLUS & DMULT prompts the question: What does DPOW do?

As repeated addition of n to n produces n, this possibility is discarded as being too dull even for dismals.

Conservatively, repeated multiplication looks like a promising candidate. Here again, single digit powering is of little interest, as eg

5 d^ 300000001 = 5

but for integers > 9 the results become interesting - hopefully not so interesting
that the operation is not regarded as dismal.

So here's a provisional programme using the provisional definition of Dpowering - I name the programme DPOW.

Enter positive integer n to power to positive integer power p, eg

123
4

produces

111122223.

DPOW

Code:
::   CK2&Dispatch   # FFFF   ::     FPTR2 ^DupQIsZero?     case2drop     Z1_     FPTR2 ^DupZIsOne?     caseDROP     OVER     PTR 2F3A3     %2     %<     caseDROP     OVERUNROT     FPTR2 ^Z>ZH     FPTR2 ^ZBits     #1-     ZERO_DO     SWAPDUP     ID DMULT     SWAP     ISTOP-INDEX     #1-     FPTR2 ^ZBit?     IT     ::       SWAP3PICK       ID DMULT       SWAP     ;     LOOP     DROPSWAPDROP   ; ;

Alternative definitions of Dpowering welcome, programmes too.
05-13-2015, 05:59 PM
Post: #24
 Dave Britten Senior Member Posts: 2,168 Joined: Dec 2013
RE: Dismal Arithmetic & 3 Progs for 49G & Others
Damn it, Gerald, you're just trying to get me to rewrite all those goofy ports I did. ;D

Actually, this would be pretty simple to add to any of the existing programs. You just need to store another loop counter, a copy of the original base, and depending on the language, a running total, then just stick in an outer loop that calls the multiply routine repeatedly.

Part of me says, yeah, that's a sensible definition of powering, since it maintains x^2 = x*x, but for these operations, x+x != 2*x. So I guess it could reasonably go either way, really.
05-13-2015, 06:37 PM
Post: #25
 Gerald H Senior Member Posts: 1,567 Joined: May 2014
RE: Dismal Arithmetic & 3 Progs for 49G & Others
With my definition & my programme 123 d^ 30 takes 11.6 sec on the 50G, so simply multiplying n times will take a truly dismally long time.
05-13-2015, 07:26 PM
Post: #26
 Dave Britten Senior Member Posts: 2,168 Joined: Dec 2013
RE: Dismal Arithmetic & 3 Progs for 49G & Others
(05-13-2015 06:37 PM)Gerald H Wrote:  With my definition & my programme 123 d^ 30 takes 11.6 sec on the 50G, so simply multiplying n times will take a truly dismally long time.

True. We're probably looking at something like O(n log n) complexity for this. Good reason not to try it on my TI-66.
10-28-2015, 02:17 PM
Post: #27
 Gerald H Senior Member Posts: 1,567 Joined: May 2014
RE: Dismal Arithmetic & 3 Progs for 49G & Others
Two programmes for the HP 35S, a translation of Dave Britten's 32S programmes above.

Given stack levels X & Y integers, programme A returns dismal sum & programme M returns dismal product.

Code:
 Addition 1    LBL A 2    STO B 3    XEQ A038 4    x<>y 5    STO A 6    XEQ A038 7    x<y? 8    x<>y 9    3 10    10^x 11    / 12    1 13    + 14    STO D 15    0 16    STO C 17    RCL A 18    RCL D 19    IP 20    XEQ A046 21    RCL B 22    RCL D 23    IP 24    XEQ A046 25    x<y? 26    x<>y 27    RCL D 28    IP 29    1 30    - 31    10^x 32    * 33    STO+ C 34    ISG D 35    GTO A017 36    RCL C 37    RTN 38    x=0? 39    RTN 40    ABS 41    LOG 42    1 43    + 44    IP 45    RTN 46    1 47    - 48    10^x 49    / 50    IP 51    10 52    / 53    FP 54    10 55    * 56    RTN Multiplication 1    LBL M 2    STO H 3    XEQ A038 4    STO J 5    x<>y 6    STO G 7    XEQ A038 8    3 9    10^x 10    / 11    STO K 12    0 13    STO I 14    0 15    STO L 16    RCL H 17    RCL J 18    IP 19    XEQ A046 20    STO M 21    RCL K 22    FP 23    1 24    + 25    STO K 26    RCL M 27    RCL G 28    RCL K 29    IP 30    XEQ A046 31    x>y? 32    x<>y 33    RCL K 34    IP 35    1 36    - 37    10^x 38    * 39    STO+ L 40    ISG K 41    GTO M026 42    10 43    RCL* I 44    RCL L 45    XEQ A001 46    STO I 47    DSE J 48    GTO M014 49    RCL I 50    RTN
10-30-2015, 10:43 AM
Post: #28
 Gerald H Senior Member Posts: 1,567 Joined: May 2014
RE: Dismal Arithmetic & 3 Progs for 49G & Others
Here a copy of Dave Britten's programmes for the HP 42S.

DIS+ performs dismal addition, DIS* dismal multiplication.

Code:
 0.    { 174-Byte Prgm } 1.    LBL “DIS+” 2.    STO 02 3.    XEQ 00 4.    X<>Y 5.    STO 01 6.    XEQ 00 7.    X<Y? 8.    X<>Y 9.    1E3 10.    / 11.    1 12.    + 13.    STO 04 14.    0 15.    STO 03 16.    LBL 01 17.    RCL 01 18.    RCL 04 19.    IP 20.    XEQ 02 21.    RCL 02 22.    RCL 04 23.    IP 24.    XEQ 02 25.    X<Y? 26.    X<>Y 27.    RCL 04 28.    IP 29.    1 30.    - 31.    10^X 32.    * 33.    STO+ 03 34.    ISG 04 35.    GTO 01 36.    RCL 03 37.    RTN 38.    LBL 00 39.    X=0? 40.    RTN 41.    ABS 42.    LOG 43.    1 44.    + 45.    IP 46.    RTN 47.    LBL 02 48.    1 49.    - 50.    10^X 51.    / 52.    IP 53.    10 54.    / 55.    FP 56.    10 57.    * 58.    RTN 59.    LBL "DIS*" 60.    STO 06 61.    XEQ 00 62.    STO 08 63.    X<>Y 64.    STO 05 65.    XEQ 00 66.    1E3 67.    / 68.    STO 09 69.    0 70.    STO 07 71.    LBL 04 72.    0 73.    STO 10 74.    RCL 06 75.    RCL 08 76.    IP 77.    XEQ 02 78.    STO 11 79.    RCL 09 80.    FP 81.    1 82.    + 83.    STO 09 84.    LBL 03 85.    RCL 11 86.    RCL 05 87.    RCL 09 88.    IP 89.    XEQ 02 90.    X>Y? 91.    X<>Y 92.    RCL 09 93.    IP 94.    1 95.    - 96.    10^X 97.    * 98.    STO+ 10 99.    ISG 09 100.    GTO 03 101.    10 102.    RCL* 07 103.    RCL 10 104.    XEQ "DIS+" 105.    STO 07 106.    DSE 08 107.    GTO 04 108.    RCL 07 109.    END
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)