Entry
TSE: Math: Number: Integer: Add: How to possibly add very long integers?
Jan 23rd, 2004 06:22
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 21 January 2004 - 01:54 am --------------------
TSE: Math: Number: Integer: Add: How to possibly add very long
integers?
---
Method: store the integers in strings, and add this integers by adding
its integers from right to left
---
The method used here mimics exactly the way yourself add 2 integer
numbers.
---
That is:
1. you start from the right,
2. take the digits at that position from both numbers.
3. add them, together with a carry (0 or 1),
4. possibly subtract 10 from this sum, if more than 9
5. and put the resulting digit in the result at that position.
6. then you repeat steps 2 to 6 for the remaining digits.
7. If the last digit, you put the sum unchanged in the result
---
---
e.g.
12341234
5678903 +
-----------
18020137
---
---
Note:
The maximum length of this integers is currently limited
to the maximum length of the string, thus about 255 characters.
Or thus about 255 digits.
---
---
--- cut here ---------------------------------------------------------
FORWARD PROC Main()
FORWARD STRING PROC FNStringGetMathAddIntegerTwoS( STRING s1, STRING
s2 )
// --- MAIN --- //
PROC Main()
Warn( FNStringGetMathAddIntegerTwoS( "90", "1" ) ) // gives "91"
Warn( FNStringGetMathAddIntegerTwoS
( "3135814134123999", "513123413134123412341412342" ) ) //
gives "513123413137259226475536341"
Warn( FNStringGetMathAddIntegerTwoS
( "12341234123413413241", "99897123412341234123412341341234" ) ) //
gives "99897123412353575357535754754475"
END
<F12> Main()
// --- LIBRARY --- //
// library: string: get: math: add: integer: two
(filenamemacro=getstitw.s) [kn, ri, th, 22-01-2004 3:06:41]
STRING PROC FNStringGetMathAddIntegerTwoS( STRING in1S, STRING in2S )
// e.g. PROC Main()
// e.g. Warn( FNStringGetMathAddIntegerTwoS( "90", "1" ) ) //
gives "91"
// e.g. Warn( FNStringGetMathAddIntegerTwoS
( "3135814134123999", "513123413134123412341412342" ) ) //
gives "513123413137259226475536341"
// e.g. Warn( FNStringGetMathAddIntegerTwoS
( "12341234123413413241", "99897123412341234123412341341234" ) ) //
gives "99897123412353575357535754754475"
// e.g. END
// e.g.
// e.g. <F12> Main()
INTEGER restI = 0
INTEGER lengthminI = 0
INTEGER lengthmaxI = 0
INTEGER sumI = 0
INTEGER I = 0
INTEGER J = 0
STRING sumS[255] = ""
STRING minS[255] = ""
STRING maxS[255] = ""
STRING cminS[1] = ""
STRING cmaxS[1] = ""
IF Length( in1S ) < Length( in2S )
minS = in1S
maxS = in2S
ELSE
minS = in2S
maxS = in1S
ENDIF
lengthminI = Length( minS )
lengthmaxI = Length( maxS )
FOR I = 1 TO lengthmaxI
J = lengthminI - I + 1
IF J > 0
cminS = minS[ J ]
ELSE
cminS = "0"
ENDIF
cmaxS = maxS[ lengthmaxI - I + 1 ]
sumI = Val( cmaxS ) + Val( cminS ) + restI
IF sumI > 9 AND ( I <> lengthmaxI )
restI = sumI / ( 9 + 1 )
sumI = sumI MOD ( 9 + 1 )
ELSE
restI = 0
ENDIF
sumS = Str( sumI ) + sumS
ENDFOR
RETURN( sumS )
END
--- cut here ---------------------------------------------------------
---
---
Internet: see also:
---
TSE: Math: Number: Operation: Overview: Can you give an overview of
long digit operations in TSE?
http://www.faqts.com/knowledge_base/view.phtml/aid/28481/fid/1629
----------------------------------------------------------------------