Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number limit 2

Status
Not open for further replies.

lived

Programmer
Jun 6, 2000
39
0
0
CA
i'm doing a calcul program and when i'm multipliing or divising number a get a number with an exposant.&nbsp;&nbsp;ex:1283450E+2. or 1.002344E-3 there is a way to allow variable to get more digits.<br>tanx in advance <p>miguel<br><a href=mailto:migoul@hotmail.com>migoul@hotmail.com</a><br><a href= page (not been updated recently)</a><br>
 
typically not, normally when you get exponitial increases there is just too much data for it to handle, and requires representing it that way, so even if you tried to increase the decimal range, you would just end up with zeros on all the parts you didnt see before, by the way what type are you storing it in? Also have you looked at the print using# in the help file, I hear it has some pretty usefull options in formating your output. <p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
A dear genius friend of mine, recently deceased, wrote a BASIC program to find all of the prime numbers (an impossible task, obviously). The program was printing out numbers more than a hundred digits in length on the day that he died.<br><br>I had asked him how he managed to get his rather primitive computer to handle numbers that large and he told me it was easy &quot;You just do it one digit at a time.&quot;<br><br>If you have two very large numbers (say 10 to 100 digits) convert them into strings and do the math one digit at a time. Try this: grab a piece of paper and a pencil. Write down two large numbers and start multiplying them by hand.<br><br>Watch your hand and your brain. Your program will have to duplicate those steps.<br><br><br><br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
may I ask how you asked him that question, or was it prior to death? <p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
Here's an example of the BASIC math my friend told me about. The task here is to simply add two numbers and print the result.<br>It wouldn't take a huge leap of imagination to apply the method to subtraction, multiplication or division.<br><br>'Start with two rather large numbers. These numbers can be almost any size as long as they and the resulting &quot;Answer&quot; string don't exceed the string limitations imposed by Qbasic.<br><FONT FACE=monospace><b><br>CLS<br>Num1$ = &quot;5882451358123631651151861331133779121514779413347&quot;<br>Num2$ = &quot;819447512325887801515201452014235892514689231&quot;<br>IF LEN(Num2$) &gt; LEN(Num1$) THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;Num1$ = STRING$(LEN(Num2$) - LEN(Num1$), &quot;0&quot;) + Num1$<br>END IF<br>IF LEN(Num1$) &gt; LEN(Num2$) THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;Num2$ = STRING$(LEN(Num1$) - LEN(Num2$), &quot;0&quot;) + Num2$<br>END IF<br>Carry$ = &quot;0&quot;<br>FOR R = LEN(Num2$) TO 1 STEP -1<br>&nbsp;&nbsp;&nbsp;&nbsp;A$ = LTRIM$(STR$(VAL(MID$(Num1$, R, 1)) +&nbsp;&nbsp;_<br>&nbsp;&nbsp;&nbsp;&nbsp;VAL(MID$(Num2$, R, 1)) + VAL(Carry$)))<br>&nbsp;&nbsp;&nbsp;&nbsp;IF LEN(A$) &gt; 1 THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;Carry$ = LEFT$(A$, 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;ELSE<br>&nbsp;&nbsp;&nbsp;&nbsp;Carry$ = &quot;0&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>&nbsp;&nbsp;&nbsp;&nbsp;Answer$ = RIGHT$(A$, 1) + Answer$<br>NEXT<br>PRINT &quot;&nbsp;&nbsp;&quot;; Num1$<br>PRINT &quot;+ &quot;; Num2$<br>PRINT &quot;&nbsp;&nbsp;&quot;; STRING$(LEN(Num2$), &quot;-&quot;)<br>PRINT &quot;&nbsp;&nbsp;&quot;; Answer$<br>PRINT : PRINT &quot;...OR...&quot;<br>PRINT VAL(Num1$) + VAL(Num2$)<br></font></b><br>The concept of applying this method to a routine that finds and stores 100-digit prime numbers is way beyond my simple grasp of math but, someday, another bored programmer will attempt it on a Commodore 64 and, perhaps, inspire the rest of us the way Paul inspired me.<br><br>And Karl... your question... well I think you can figure that one out.<br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Alt255 --<br><br>That is the most ingenious way I have ever seen it done.&nbsp;&nbsp;I've never really conseptulized seeing beyond the QB limits.&nbsp;&nbsp;But that is something. <p> <br><a href=mailto: > </a><br><a href= > </a><br>Secrete of life: If Husband = Happy then Wife = Unhappy. If Wife = Happy then Husband = Unhappy. If Parents = Happy then Kids = Unhappy. If Kids = Happy then Parents = Unhappy.
 
Your code works great, but you should add this line to it right before all the print statements:<br><br><FONT FACE=monospace>IF Carry$&lt;&gt;&quot;0&quot; then Answer$=Carry$+Answer$</font><br><br>This checks to see if the numbers carried over one more place.
 
DOH! I missed that one, Rogue2. My code only works if one of the numbers has more digits than the other or the sum of the left-most digits plus the carry is less than ten. You could check for Carry$ &lt;&gt; &quot;0&quot; or add a &quot;0&quot; at the beginning of both numbers. But your solution is much more efficient.<br><br>I've thought about using Paul's &quot;string math&quot; to perform some truly astronomical calculations but, of course, realized that the use of this simple addition function would require a full compliment of other math functions to be useful. I tried to create a MULTIPLYhugeNumXhugeNum(Num1$, Num2$) function and caught myself trying to add X to X, Y number of times. Not very efficient (but a good example of the way my math-deprived brain performs multiplication... using my fingers as markers. My function mirrored the movements of my fingers.)<br><br>Has anybody thought of a reliable way to multiply a thousand-digit number by a two-digit number? I know it can be done.<br><br>Thanks in advance,<br>Alt255<br><br><br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Here's a suppliment to the quest for &quot;huge number&quot; QB math. The Multiply$ function listed here multiplies the values in Num1$ and Num2$ and prints a 100-digit solution.<br>I'm sorry if I couldn't use my calculator to verify the accuracy. &lt;grin&gt;<br><b><FONT FACE=monospace><br>DEFINT A-Z<br>DECLARE FUNCTION Multiply$ (Num1$, Num2$)<br>CLS<br><br>Num1$ = &quot;583416511508123631651151861331133779121514779413347&quot;<br>Num2$ = &quot;4357489407512325887801515201452014235892514689231&quot;<br><br>PRINT Multiply$(Num1$, Num2$)<br>PRINT : PRINT &quot;...or settle for...&quot;<br>PRINT VAL(Num1$) * VAL(Num2$)<br><br>FUNCTION Multiply$ (Num1$, Num2$) </b></font><br>'Format the number strings so they'll be easy to use... <b><FONT FACE=monospace><br>IF LEN(Num2$) &gt; LEN(Num1$) THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;Num1$ = STRING$(LEN(Num2$) - LEN(Num1$), &quot;0&quot;) + Num1$<br>END IF<br>IF LEN(Num1$) &gt; LEN(Num2$) THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;Num2$ = STRING$(LEN(Num1$) - LEN(Num2$), &quot;0&quot;) + Num2$<br>END IF<br>Num1$ = &quot;0&quot; + Num1$: Num2$ = &quot;0&quot; + Num2$: Carry$ = &quot;0&quot; </b></font><br>'Loop through the digits of the first number <b><FONT FACE=monospace><br>FOR R2 = LEN(Num2$) TO 1 STEP -1<br>&nbsp;&nbsp;&nbsp;&nbsp;Temp2$ = Answer$: Answer$ = &quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;Temp1$ = STRING$(ABS(R2 - LEN(Num2$)), &quot;0&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;IF MID$(Num2$, R2, 1) &lt;&gt; &quot;0&quot; THEN </b></font><br>&nbsp;&nbsp;&nbsp;&nbsp;'If we aren't trying to multipy by zero then<br>&nbsp;&nbsp;&nbsp;&nbsp;'loop through the digits of the second number. <b><FONT FACE=monospace><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FOR R1 = LEN(Num1$) TO 1 STEP -1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A$ = LTRIM$(STR$(VAL(MID$(Num1$, R1, 1)) * VAL(MID$(Num2$, R2, 1)) + VAL(Carry$)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF LEN(A$) &gt; 1 THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Carry$ = LEFT$(A$, 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ELSE<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Carry$ = &quot;0&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Temp1$ = RIGHT$(A$, 1) + Temp1$<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NEXT<br>&nbsp;&nbsp;&nbsp;&nbsp;END IF </b></font><br>&nbsp;&nbsp;&nbsp;&nbsp;'Format the temporary number strings for easy of use <b><FONT FACE=monospace><br>&nbsp;&nbsp;&nbsp;&nbsp;IF LEN(Temp2$) &gt; LEN(Temp1$) THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Temp1$ = STRING$(LEN(Temp2$) - LEN(Temp1$), &quot;0&quot;) + Temp1$<br>&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>&nbsp;&nbsp;&nbsp;&nbsp;IF LEN(Temp1$) &gt; LEN(Temp2$) THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Temp2$ = STRING$(LEN(Temp1$) - LEN(Temp2$), &quot;0&quot;) + Temp2$<br>&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>&nbsp;&nbsp;&nbsp;&nbsp;Temp1$ = &quot;0&quot; + Temp1$: Temp2$ = &quot;0&quot; + Temp2$: Carry$ = &quot;0&quot; </b></font><br>&nbsp;&nbsp;&nbsp;&nbsp;'Add the temporary strings together <b><FONT FACE=monospace><br>&nbsp;&nbsp;&nbsp;&nbsp;FOR R = LEN(Temp2$) TO 1 STEP -1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A$ = LTRIM$(STR$(VAL(MID$(Temp1$, R, 1)) + VAL(MID$(Temp2$, R, 1)) + VAL(Carry$)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF LEN(A$) &gt; 1 THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Carry$ = LEFT$(A$, 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ELSE<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Carry$ = &quot;0&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Answer$ = RIGHT$(A$, 1) + Answer$<br>&nbsp;&nbsp;&nbsp;&nbsp;NEXT<br>NEXT<br></b></font><br>'Strip off the leading zeros...<br><b><FONT FACE=monospace><br>FOR Re = 1 TO LEN(Answer$)<br>&nbsp;&nbsp;&nbsp;&nbsp;IF MID$(Answer$, Re, 1) &lt;&gt; &quot;0&quot; THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Answer$ = RIGHT$(Answer$, LEN(Answer$) - Re + 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EXIT FOR<br>&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>NEXT<br><br>Multiply$ = Answer$<br></b></font><br>'This is your 100-digit answer.<br>'It should require a fraction of a second on modern systems but I would hate to try it with two 4000-digit numbers. You might have to wait a while... go out for dinner and a movie.<br><b><FONT FACE=monospace><br>END FUNCTION<br></b></font><br><br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Hey Alt255,<br><br>Here's my version of Large Multiply. (note I constructed around your request of having 1000 digits times by 2 digits.&nbsp;&nbsp;But then I thought why limit to 2? So I've increased the input to receive 5 instead.<br><br>(1000 (numbers) * 5 digits)<br><br>And, as you said, sorry if I don't acutally use a calculator to verify accuracy. &lt;smile&gt;<br><br>----&lt;program code&gt;----<br>'generate 1000 psudoe numbers and store in String X$<br><b>RANDOMIZE (TIMER)<br>CLS<br>FOR Cycle = 1 TO 1000<br>&nbsp;&nbsp;&nbsp;&nbsp;PsNum$ = LTRIM$(STR$(INT(RND * 9)))<br>&nbsp;&nbsp;&nbsp;&nbsp;MigNum1$ = MigNum1$ + PsNum$<br>&nbsp;&nbsp;&nbsp;&nbsp;CLS<br>&nbsp;&nbsp;&nbsp;&nbsp;PRINT MigNum1$<br>&nbsp;&nbsp;&nbsp;&nbsp;'FOR SlowDown = 1 TO 10000: NEXT<br>NEXT</b><br><br><br>'show number<br><b>CLS<br>PRINT &quot;numbers generated for multiplication:&quot;<br>PRINT MigNum1$</b><br><br><br>'prove len<br><b>PRINT<br>PRINT &quot;number of digits above:&quot;; LEN(MigNum1$)</b><br>'prove Exp: E+/D+<br><b>PRINT<br>PRINT &quot;Exponetion # is :&quot;; VAL(MigNum1$)<br>SLEEP 2<br></b><br><br>'get upto 5 digits to Multiply by<br><b>PRINT<br>INPUT &quot;Enter up to 5 digits to times by: &quot;, MigNum2$<br>IF LEN(MigNum2$) &gt; 5 THEN MigNum2$ = RIGHT$(MigNum2$, 5)</b><br><br><br>'create arrays & store &quot;0&quot;s to end<br><b>DIM MigTemp$(LEN(MigNum2$))<br>IF UBOUND(MigTemp$) &gt;= 2 THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;FOR LowSect = LEN(MigNum2$) TO 2 STEP -1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MigTemp$(LowSect) = STRING$(LowSect - 1, &quot;0&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;NEXT LowSect<br>END IF</b><br><br><br><font color=red>MultiplySection:</font><br><b>FOR LowSect = LEN(MigNum2$) TO 1 STEP -1<br>&nbsp;&nbsp;&nbsp;&nbsp;WorkLowNum = VAL(MID$(MigNum2$, LowSect, 1))<br>&nbsp;&nbsp;&nbsp;&nbsp;CarryOvr = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;Array = LowSect<br>&nbsp;&nbsp;&nbsp;&nbsp;FOR HighSect = LEN(MigNum1$) TO 1 STEP -1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WorkHighNum = VAL(MID$(MigNum1$, HighSect, 1))</b><br><br>'---Multiply and add stuff---<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WorkResult = (WorkLowNum * WorkHighNum) + CarryOvr<br></b><br>'---logic routines---<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF LEN(LTRIM$(STR$(WorkResult))) = 2 THEN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Max 9x9=81<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MigTemp$(Array) = RIGHT$(LTRIM$(STR$(WorkResult)), 1) + MigTemp$(Array)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CarryOvr = VAL(LEFT$(LTRIM$(STR$(WorkResult)), 1))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ELSEIF LEN(LTRIM$(STR$(WorkResult))) = 1 THEN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Min 1x9=9 & 1x0=0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MigTemp$(Array) = LTRIM$(STR$(WorkResult)) + MigTemp$(Array)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CarryOvr = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>&nbsp;&nbsp;&nbsp;&nbsp;NEXT HighSect</b><br>'---add carry overs to front if no more to times by---<br><b>&nbsp;&nbsp;&nbsp;&nbsp;IF CarryOvr &lt;&gt; 0 THEN MigTemp$(Array) = LTRIM$(STR$(CarryOvr)) + MigTemp$(Array)<br>NEXT LowSect</b><br><br><font color=red>AdditionSection:</font><br>'---per ALT255's code---Small modifications my MIGGYD---<br>'***NOTE--I'm using Dim #0 for storage...therefore, don't OPT BASE this code<br>'***if you do then you'll have to modify this module.<br><b>FOR Cycle = 1 TO LEN(MigNum2$)<br>&nbsp;&nbsp;&nbsp;&nbsp;MigNum1$ = MigTemp$(0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Store current total<br>&nbsp;&nbsp;&nbsp;&nbsp;MigNum2$ = MigTemp$(Cycle)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Store total from next array for add'g<br>&nbsp;&nbsp;&nbsp;&nbsp;MigTemp$(0) = &quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'clear the storage array for use later<br>&nbsp;&nbsp;&nbsp;&nbsp;IF LEN(MigNum2$) &gt; LEN(MigNum1$) THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MigNum1$ = STRING$(LEN(MigNum2$) - LEN(MigNum1$), &quot;0&quot;) + MigNum1$<br>&nbsp;&nbsp;&nbsp;&nbsp;ELSEIF LEN(Num1$) &gt; LEN(Num2$) THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MigNum2$ = STRING$(LEN(MigNum1$) - LEN(MigNum2$), &quot;0&quot;) + MigNum2$<br>&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>&nbsp;&nbsp;&nbsp;&nbsp;CarryOvr$ = &quot;0&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;FOR Rep = LEN(MigNum2$) TO 1 STEP -1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A$ = LTRIM$(STR$(VAL(MID$(MigNum1$, Rep, 1)) + VAL(MID$(MigNum2$, Rep, 1)) + VAL(Carry$)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF LEN(A$) &gt; 1 THEN<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Carry$ = LEFT$(A$, 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ELSE<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Carry$ = &quot;0&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END IF<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MigTemp$(0) = RIGHT$(A$, 1) + MigTemp$(0)<br>&nbsp;&nbsp;&nbsp;&nbsp;NEXT<br>&nbsp;&nbsp;&nbsp;&nbsp;IF Carry$ &lt;&gt; &quot;0&quot; THEN MigTemp$(0) = Carry$ + MigTemp$(0)<br>&nbsp;&nbsp;&nbsp;&nbsp;PRINT<br>&nbsp;&nbsp;&nbsp;&nbsp;PRINT &quot;&nbsp;&nbsp;&quot;; MigNum1$<br>&nbsp;&nbsp;&nbsp;&nbsp;PRINT &quot;+ &quot;; MigNum2$<br>&nbsp;&nbsp;&nbsp;&nbsp;PRINT STRING$(80, &quot;-&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;PRINT MigTemp$(0)<br>&nbsp;&nbsp;&nbsp;&nbsp;PRINT : PRINT &quot;...OR...&quot;: PRINT<br>&nbsp;&nbsp;&nbsp;&nbsp;PRINT VAL(MigTemp$(0)): PRINT<br>&nbsp;&nbsp;&nbsp;&nbsp;SLEEP 1<br>NEXT Cycle<br>PRINT &quot;total is: &quot;<br>PRINT MigTemp$(0)<br>PRINT : PRINT &quot;...OR...&quot;: PRINT<br>PRINT VAL(MigTemp$(0))<br>END</b><br>---&lt;end program code&gt;---<br><br>Obviously, you can use LPRINT to have a hardcopy for review.<br>Comments? Errors? Let me know, please. <p> <br><a href=mailto: > </a><br><a href= > </a><br>By the twitching of my thumb, something wicked this way comes........oh, it's only a boogy.
 
Good work, MiggyD. I tried your code (after removing the print statements in the FOR/NEXT loop) then used the TIMER function get get an idea of how fast the multiplication section ran. It came in at <b>.759765625 seconds</b> (the TIMER isn't known for its great accuracy but it gave me &quot;a&quot; number to use for comparison).<br><br>Then I added your 1000-digit random string generator to my code, fixed a bug I hadn't noticed... the function kept trying to multiply the leading zero strings in Num2$ so I took the original length of Num2$ (<FONT FACE=monospace>Num2Size& = LEN(Num2$)</font>) and changed one of the FOR statements to read <br><FONT FACE=monospace>FOR R2 = LEN(Num2$) TO LEN(Num2$) - Num2Size& STEP -1</font>.<br><br>My code executed in <b>1.220703125 seconds</b> (substantially slower)<br><br>I like your way better (for obvious reasons). Now... how can we coerce your code into multiplying <i><b>any</b></i> two numbers?<br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Alt255,<br><br>Firstly, I didn't give a second thought to speed--I just wanted to see if I could do it.&nbsp;&nbsp;I don't necessarily need speed on my home computer, but may be if you needed this code for work or something then I could see where speed is a factor.<br><br>Secondly, you could use an input statment in place of the <b>psudeo generator</b> and ammend the <b>input upto 5 digits</b>.<br><br>Originally, I conceived how to input upto 1000 digits (took too long to enter them) in to a string var so I had a FOR/NEXT statement do it for me.<br><br>Approximately, how many digits are we talking about??&nbsp;&nbsp;<br><br>Because QB (I have 4.5) allows one string variable to contain upto only 32,767 chars. which I believe is just about enough numbers to work with.<br><br>And, since I'm using dims to store the answers for each number in Num2$, I just don't know if Qb could handel that many DIMs even with Dynamic set &quot;on&quot;.&nbsp;&nbsp;Hum...may be? have to conentate certain vars or something<br><br>(screen shot of help file)<br><FONT FACE=monospace>Limits to QuickBASIC - Arrays<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Maximum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Minimum<br>Array size (all elements)<br>&nbsp;&nbsp;Static&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;65,535 bytes (64 K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>&nbsp;&nbsp;Dynamic&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Available memory<br>Number of dimensions allowed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;60&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>Dimensions allowed if unspecified&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>Array subscript value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;32,767&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-32,768<br><br>&nbsp;&nbsp;Note: The maximum range between array subscript values is 32,767<br></font><br><br>If you want me to post some more code on this, let me know. <p> <br><a href=mailto: > </a><br><a href= > </a><br>By the twitching of my thumb, something wicked this way comes........oh, it's only a boogy.
 
Thanks for the reply, MiggyD. The need for speed is indeed a work requirement (my day-job is in an environment where high precision is very desirable and my night-job - &quot;home work&quot;, if you will - is currently focused on a project that requires deadly precision in huge numbers.<br><br>No, I'm not a physicist trying to account for every electron in the universe. I'm just working on an anti-crack module for some software that my wife thinks will support us in our old age. I don't think the final version will be written in QB (but it's <i>possible!</i>)<br><br>Anyway... this discussion has been very helpful to me and I hope it has stimulated other members to explore the possibilities. Qbasic might not be the latest or greatest language but it is accessible to the most users. Its flexibility fosters a creative urge in beginners that would be quickly stifled by the more &quot;advanced&quot; languages.<br><br>Remember the guy who wanted to create a new operating system with Qbasic? I don't doubt he will succeed. The OS won't be written in Qbasic but Qbasic opened his eyes and will always remind him that the possibilities are without limit.<br><br><br><br> <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Seem like this Forum will stand still for a while, Alt255!<br>I joined a few weeks ago and it seems like many stars had born since.<br><br>Keep going on that way, folks!!!<br>
 
Alt255,

Yes, I remember the person wanting to create an OS; and I'm sure that most of us (the more seasoned ones) are rooting for him--I know I'd like to see where his preseverence leads him.

Good luck with your nest egg. I would not recommend you write your program in QB if it could be helped, but that's just my personal opinion. I mean what would have been the time frame (Oh...I don't know) to, let's say, write in the code for the program ACCESS in QB than to use say C or something else? Maybe a year more than was really needed? Think of your audiance (they'll most likely be using Windows 3,000,000 by then and QB doesn't interface with 32 bit systems all that great (or 128 bits if using Win 3-Million [hehe]). But seriously good luck, hope I brought back some memories for you and made you laugh a Bit or 128 of them [hehe].

But getting back to QB programming...I'm trying to figuer something out...how many digits did you enter into NUM1$ (or in my case MigNum1$) and how many digits did you enter into Num2$ (or MigNum2$)?

And then I thought, how come my code is faster than yours? and I thought that you may have only entered the 5 digits in as the number for NUM2$ instead of a really really large number.

So, on that train of thought, I pasted your &quot;ADD LARGE&quot; 's NUM1$ and NUM2$ into my code, removed (actually REM'd) the number generator and the 3 lines for inputing upto 5 digits so that NUM2$ would be indicitive of the NUM2$ in your add code.

After running the numbers (and removing (actually relocated) the print statements) a few times it averaged out to be approx 2.0 seconds (the lowest I got was approx. 1.8 seconds). AND, when I'd enter upto 5 digits it would average out to about .64-.68 seconds--see PPS. Again, I believe that the speed is relevant to the number of digits in NUM2$; but, I want to confirm it with you that the lower number of digits to work with will infact increase result response time.

Please respond to the above paragraph. I'm going to make it a point to check out this post daily for the next 7 days or so.

PS--I'm also conceiving a modification to my code to maybe push the speed (dependening of the nubmers to multiplay against) a little bit faster. More details later, but you may like it--if it works that is.

PPS--My PC: 16 megs, 486DX (so internal math proc.), slushing through at 75Hz...don't know if that's a difference in processing speed also, FYI.
 
MiggyD, you made me laugh in full 256 bit mode (I guess that's one more than Alt255 deserves).

Regarding the speed thing: I used 1000 random digits in Num1$ and 5 random digits in Num2$ (didn't use a input statement). I tested the functions 10 times and posted the averages. Computer = AMD 350 MHz w/ 64MB, Win 98. I repeated the tests on one of my work machines (AMD 500 MHz w/ 128MB, Win 98 SE) and had similar results. Odd... but stranger still, I wrote equivalent VB6 functions to emulate both sets of code, ran them on the faster machine and found that both executed in about the same amount of time (BTW, both, with a few modifications, multiplied two 1000 digit numbers in about 47 seconds).

This is a true head scratcher. Perhaps Windows is the great equalizer... all code, slow or fast, runs at the same speed. (?????)

I'm pretty sure that fewer digits in Num2$ will result in faster performance across the board but, aside from my comparisons of 1000x5 and 1000x1000 test runs I don't have any experimental data to support that assumption.

I'm looking forward to seeing your modifications. This is getting very interesting.



 
If you need arbitrary precision arithmetic, use scheme, a dialect of lisp. I remember easily handled 80! = (eighty factorial) =~ 7x10^118 There are plenty of fast arbitrary precision packages in C too.
 
Alt255--

Your first line made ME laugh, too. I'm glad it did. I had a hard day.

Anyways, I way able to get some more speed in the Multiplication Section of the above code.

Testing: I maintained using 1000 x 5. However, I used one thousand 9's times against five 9's [using highest numbers is best for worst case seniaros, I believe] and came to a baseline for both the original code and the modified one. Only my code was modified, I'll try to get to yours as soon as I can, unless someone else has already, so you can paste the following code over the Multiplication section completely, and it will still function as before only a little bit faster.

Here's some of the details. Just remember that my system is a 486DX (obsolete really). And you kind of scared me with the 1000 x 1000 numbers, whew! That's alot of numbers...Are you sure you're not trying to find the origins of the Univers????[hehe]. Anyway, I hope this helps a little.

Original Code:
Multiply speed=1.75

Addition speed=1.87
Over all speed=3.57

Modified Code:
Multiply Speed= .88

Addition Speed=1.869
Over all speed=2.69

Instead of multiplying just 1(Num1) digit against 1(Num2) why not multiply 2(num1) by 1(num2)? {I didn't want to try 3 x 1 cause it may slow things down--see red star below.}

[red]MultiplySection:[/red]
FOR LowSect = LEN(MigNum2$) TO 1 STEP -1
WorkLowNum = VAL(MID$(MigNum2$, LowSect, 1))
CarryOvr = 0
Array = LowSect

'++++begin test block++++
'--get first 2 numbers in MigNum1$, work them, set CarryOvr (as needed)
'--and setup cycling routine for rest of numbers
HighSect = LEN(MigNum1$) - 2
WorkHighNum = VAL(RIGHT$(MigNum1$, 2))

WorkResult = (WorkLowNum * WorkHighNum) + CarryOvr

IF LEN(LTRIM$(STR$(WorkResult))) = 3 THEN
'
'--max possible result 3 digits
'--9 x 99 = 891 (keep 91, carry 8)
'
MigTemp$(Array) = RIGHT$(LTRIM$(STR$(WorkResult)), 2) + MigTemp$(Array)
CarryOvr = VAL(LEFT$(LTRIM$(STR$(WorkResult)), 1))
ELSEIF LEN(LTRIM$(STR$(WorkResult))) = 2 THEN '
'
'--intermediate results 2 digits
'--1 x 99 = 99
'
MigTemp$(Array) = LTRIM$(STR$(WorkResult)) + MigTemp$(Array)
CarryOvr = 0
ELSEIF LEN(LTRIM$(STR$(WorkResult))) = 1 THEN
'
'--Minimum possible results 1 digit
'--1 x 00 = 0 & 1 x 09 = 9[red]*[/red]
'--NOTE: #1...09 = 9 (leading 0 is removed from disply & results therefore add the carry over in front)
'-- #2...00 = 0 (only one 0 is returned, place Carry Over in front that, there's no need to write in more code to fill in the additional spaces*note it may slow prg.*)
'
MigTemp$(Array) = &quot;0&quot; + LTRIM$(STR$(WorkResult)) + MigTemp$(Array)
CarryOvr = 0
END IF

FOR HighSect = HighSect TO 1 STEP -2
WorkHighNum = VAL(MID$(MigNum1$, HighSect, 2))

'---Multiply and add stuff---
WorkResult = (WorkLowNum * WorkHighNum) + CarryOvr

'---logic routines---
IF LEN(LTRIM$(STR$(WorkResult))) = 3 THEN 'Max 9x99=891
MigTemp$(Array) = RIGHT$(LTRIM$(STR$(WorkResult)), 2) + MigTemp$(Array)
CarryOvr = VAL(LEFT$(LTRIM$(STR$(WorkResult)), 1))
ELSEIF LEN(LTRIM$(STR$(WorkResult))) = 2 THEN 'Min 1x9=99
MigTemp$(Array) = LTRIM$(STR$(WorkResult)) + MigTemp$(Array)
CarryOvr = 0
ELSEIF LEN(LTRIM$(STR$(WorkResult))) = 1 THEN 'Min 1x00=0 & 1x09=9
MigTemp$(Array) = &quot;0&quot; + LTRIM$(STR$(WorkResult)) + MigTemp$(Array)
CarryOvr = 0
END IF
'+++end test block+++


NEXT HighSect

'---add carry overs to front if no more to times by---
IF CarryOvr <> 0 THEN MigTemp$(Array) = LTRIM$(STR$(CarryOvr)) + MigTemp$(Array)
NEXT LowSect

All the bold stuff is modified code the rest (what little there is of it) is the same as before. Please let me know if it readly does speed up on your system [red]OR[/red] if you have any problem/questions/comments.

Enjoy!
 
i know i am joining in a little late, but this multiply large numbers thing was an assignment of mine back when i was taking qbasic in high school (just a couple years ago). It was part of a 20 problem semester exam that we had 3 weeks to do. that was the one problem I never figured out how to do, and haven't thought of it since then. kind of neat that you guys still use basic to do neat things like this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top