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!

way for not using ltrim$ rtrim$ 3

Status
Not open for further replies.

lived

Programmer
Jun 6, 2000
39
CA
i use ltrim$(rtrim$(str$(number))) to get rid of the heading and ending space that str$ is returning, and i wonder if there is not a more efficient way to do this because it take a lot more time cause i'm calculate numbers larger than 10000 characters long and its taking eternity to calculate...


FOR r = nomchi TO 1 STEP -1
tempo = (suite(r) * expo) + reste
suitetotal(r) = VAL(RIGHT$(STR$(tempo), 1))
IF LEN(LTRIM$(RTRIM$(STR$(tempo)))) > 1 THEN
reste = VAL(LEFT$(LTRIM$(RTRIM$(STR$(tempo))), LEN(LTRIM$(RTRIM$(STR$(tempo))))-1))
ELSE
reste = 0
END IF
NEXT


miguel
migoul@hotmail.com
 
I allways move it to a function:

Function Trim$(iStr As String)
Trim$ = Ltrim$(Rtrim$(iStr))
End Function

Then, you could change your code to read:

FOR r = nomchi TO 1 STEP -1
tempo = (suite(r) * expo) + reste
suitetotal(r) = VAL(RIGHT$(STR$(tempo), 1))
IF LEN(TRIM$(STR$(tempo))) > 1 THEN
reste = VAL(LEFT$(TRIM$(STR$(tempo)), LEN(TRIM$(STR$(tempo)))-1))
ELSE
reste = 0
END IF
NEXT
 
As a final step, you could reduce the number of times you use Trim$:

FOR r = nomchi TO 1 STEP -1
iTemp$ = Trim$(Str$(Tempo))
tempo = (suite(r) * expo) + reste
suitetotal(r) = VAL(RIGHT$(STR$(tempo), 1))
IF LEN(iTemp$) > 1 THEN
reste = VAL(LEFT$(iTemp$, LEN(iTemp$)-1))
ELSE
reste = 0
END IF
NEXT

Function Trim$(iStr As String)
Trim$ = Ltrim$(Rtrim$(iStr))
End Function
 
For what you're doing in the loop you've presented, you're not actually putting 10,000-digit numbers into a string. You're simply using a string to extract parts of a number stored in a regular numeric variable. You are doing two main operations: [tt]VAL(RIGHT$(temp$, 1))[/tt], which gives the units place digit, and [tt]VAL(LEFT$(temp$, LEN(temp$) - 1))[/tt], which gives the remaining digits.

Since the number stored in [tt]temp$[/tt] comes from the existing variable [tt]tempo[/tt], you can use the following numeric operations to achieve the same result much more quickly:
[ol]
[li]To retrieve the units place digit, use [tt]tempo MOD 10[/tt]. To understand why this works, you need of course to understand what the [tt]MOD[/tt] operator really is. Remember back to your elementary school days, when they taught you long division. When you finished dividing the integer part of the number, you stopped division and simply wrote the remainder next to the quotion. For example, 10 / 3 would come out 3R1, meaning a remainder of 1. The [tt]MOD[/tt] operator performs this integer division and returns the remainder instead of the quotient.[/li]
[li]To discard the units place and shift everything to the right, use [tt]tempo \ 10[/tt]. The [tt]\[/tt] operator is very similar to the [tt]/[/tt] operator, except that it, like the [tt]MOD[/tt] operator, performs an integer division. The [tt]\[/tt] operator is the partner to the [tt]MOD[/tt] operator, in that it returns the quotient that [tt]MOD[/tt] discards. If you divide 10 by 3 using the [tt]/[/tt] operator, you get 3.33333, but using the [tt]\[/tt] operator, you get only 3. Since the decimals go entirely into the remainder, which is returned by [tt]MOD[/tt] and discarded by [tt]\[/tt], you do not need to worry about the remainder being greater than half of the divider (which would traditionally cause the response to be rounded up). In other words, 5 \ 3 = 1.[/li]
[/ol]
So, you can rewrite your loop more efficiently as follows:
[tt]
FOR r = nomchi TO 1 STEP -1
tempo = (suite(r) * expo) + reste
suitetotal(r) = tempo MOD 10
reste = tempo \ 10
NEXT r
[/tt]
You should find that this offers a drastic improvement in speed :)
 
thanx you really much logiclrd. i would never thought i could reduce my code so drasticly, now it take about the half time it was for calculating! instead of taking me 10 hours to find 13 exp 12000 it will take me about 5.
did i say you thanx? ;)

miguel
migoul@hotmail.com
 
It sounds to me like you are doing this the so-called "naive" or "brute-force" way: repeatedly multiplying by thirteen 12,000 times. There is another approach that you should consider. It is sometimes called the Egyptian algorithm, since the first recorded use of it was by ancient Egyptian mathematicians.

First of all, recall some simple rules regarding exponentiation:

(a[sup]n[/sup])[sup]2[/sup] = a[sup](2*n)[/sup]
and
a[sup]n[/sup] * a[sup]m[/sup] = a[sup](n + m)[/sup]

Now, consider the act of repeatedly squaring a number x. The first time, you get x[sup]2[/sup]. The second time, you get x[sup]4[/sup]. The third time, you get x[sup]8[/sup]. Each time you square it, the exponent doubles. Now look at the second rule stated above: when multiplying two exponentiations of the same base, the result is that base to the sum of the exponents used.

When you repeatedly double a number, you get, of course, what are called the powers of 2. This is what is happening with the exponent when you repeatedly square a number. Now, the optimization comes in when you consider whether it's possible to represent the number 12,000 as a sum of powers of 2. Of course, it is, and this is merely converting the number to binary form. In fact, it is possible to represent any integer as a sum of powers of two where each power is present at most once.

The number 12,000 breaks down into the following sum of powers of two:

12000 = 8192 + 2048 + 1024 + 512 + 128 + 64 + 32

Consider the following equation:

x[sup]12000[/sup] = x[sup](8192 + 2048 + 1024 + 512 + 128 + 64 + 32)[/sup]

Using the second rule stated above, you can see that:

x[sup]12000[/sup] = x[sup]8192[/sup] * x[sup]2048[/sup] * x[sup]1024[/sup] * x[sup]512[/sup] * x[sup]128[/sup] * x[sup]64[/sup] * x[sup]32[/sup]

Consider the following chain of multiplications:

x * x = x[sup]2[/sup]
x[sup]2[/sup] * x[sup]2[/sup] = x[sup]4[/sup]
x[sup]4[/sup] * x[sup]4[/sup] = x[sup]8[/sup]
x[sup]8[/sup] * x[sup]8[/sup] = x[sup]16[/sup]
x[sup]16[/sup] * x[sup]16[/sup] = x[sup]32[/sup]
x[sup]32[/sup] * x[sup]32[/sup] = x[sup]64[/sup]
x[sup]64[/sup] * x[sup]64[/sup] = x[sup]128[/sup]
x[sup]128[/sup] * x[sup]128[/sup] = x[sup]256[/sup]
x[sup]256[/sup] * x[sup]256[/sup] = x[sup]512[/sup]
x[sup]512[/sup] * x[sup]512[/sup] = x[sup]1024[/sup]
x[sup]1024[/sup] * x[sup]1024[/sup] = x[sup]2048[/sup]
x[sup]2048[/sup] * x[sup]2048[/sup] = x[sup]4096[/sup]
x[sup]4096[/sup] * x[sup]4096[/sup] = x[sup]8192[/sup]

Here, 13 multiplications have given us all of the powers of x we need for the right-hand side of the equation. We can then perform the remaining 6 multiplications and get x[sup]12000[/sup] using only 19 multiplications. Compare this to your 12,000 multiplications.

The actual result of 13[sup]12000[/sup] is:

209039174671554512098810433216370126519699394797136504368010882893324901661 59113382136273495317149777512983676125999783574032230181747210189726375 06719885661473678586545184261023293844153559980112544413585105479568807 18114737401166511813368382581550147746483892868130462798759281932102370 02413068646892000376231003663971544799053852891766643721764862218632140 28485914429370195541455664047074298551674174128379963345730338610297640 65542353832133359038111991157027649442338372815928046469495133373885741 73575668520841061173415789664042127497228111704795127289182123915576517 80858776901743144896099616609435954106289200183180822088740804243604238 15640841416223662909743122313374967302002147227247161432079474460285372 18632352891096155529980752823362404008957069366737927828468175748677496 12128387906210612240537421574399709999314215136431711220419543639949064 14648448112772543555326288517040217465358146597106048406112671858325671 59043311015933858253488352427974615495947296773351833118248800512227386 51951535719949275859494731328526745597748699637202622718203425147411004 73570505617085854745856413587449698840649925629567787840017734938024425 39057934241124662301804052838809262703035798759848461616878338146580502 44326671401117008578865607270332962854516781822269972115972327880189720 71778671971611036088844045289444624296955574710836565381994828199287161 12443317305094492612594014238821987700440739829150609604133945398160465 60190460575324748774695436499616185154314522926036325932234875512539952 90971192766501581217996657544908753933049868324843339360438016173175252 90800367604005112924968361818061127897077137147594051948506330908268085 91998192930440566929256740197482594215985534522678451657660962890447815 33105696901244208768925900737916595406826422112695401286720229318500897 28703504371098031027980656445910799299939281970417046036329336994973075 31998134909316875510353473552181421184107999424543410343498562067006364 57404562085342730319809480348409534337469325458454827580623018780621724 06515248808964109928734307255141666169219380106563323969212438615871230 63642614333664998952984738937362210901751644776404630528057017732128719 56107574642533956806837080308196805536927210644802527851689091096124537 48382514508217719313997692161707323421710523787818989184484970278244461 80877980558749110858208797812389712097271267269206469703441599515566470 08827074856158471631196465685020537278310095902234325210820877179271859 63734556067506407471785171383383490106686336049881478099505475520248188 05912494355397889094778742317404389978989833780497361808339211690476672 25269484394568783631315816604051164088405387701207395291439595590065080 92588454713389440425207063640172954679717343507838269636180115356929898 14825295455389521306077553313728670081602880022769025605344402111607278 17568668179292222746421280798360061537862723216672324571378986787468855 14300899931952326417423767109327606773307789385815517975916799882818699 03397489287026533987179109962877735055706890041069414357947508313593425 78516766179476523438255360508997909525132558557878246573028512206621821 76019415322269113445824893304737076254250483722752429543136934307780090 12799582003610014657482356041405073163790784379592315358391047288469598 27741537414287389893463839188298984693247842099126761147458410265656143 08322004302338204778015611397659817377668610395755548155429847837805266 08105148602726261601613203627097411358812701206508763022287154741693889 92573652326788050022812365485813787747267429331963092181980703729627770 83813798412363361066091616406516593189123256666834008891879533668190916 31106705479699632349630462377679109997983040596345261159945894350083142 16429444709841419837164607243318427738405417437746352566389508874542989 97700143063048820549621967794819216402290912625118083438886099793425962 49407317384266280525283939132682563717941709112196293216001565529815495 10682849350879921926251957572303218922105757957874475178535656666314263 68264137170734777698277914149320955840495182350644282231544519483509896 45814661471463360390046316242679281114651408685600888939200829802150455 41872114839172233908982247273915258937077602426161878957856522013560174 12987125415604915928163968754466332430867181007724680620300700436166927 83234176266472942839777783229075663703511020393267864112857301752544386 31690535077687009370858986134512391428856690179433274821540699242107732 47882341625680219643066361734523239605062529959238062650758550867559448 63439160495466413573838790556018043842808743291916587613773773764615642 04678336865744688291260653845149723396222248543109318057465794843501929 78912021241445131009211483290231916468413224241214295556772501270424321 80594486486031082589014415977570698452397759568917524484894673132096330 06616277444312783779394659753657994915759200100821540821011718145557243 21704824528210245180654746961762058528260847248537175457771377824014580 14702587188380559712284608387727950339530357431075323672488923505495084 56527285691872468834423433025303169887755793566338425955288970614109822 86252531079879056783820797557446872332585411290955658913835559183881642 16588713031081673552275109568895862737757047396710054641271478691806271 96395594249809400870812750077315346065091815918802411417861663737651279 34052675678338118619808091593765890369348685879874124128313198959382675 64311744031370707443857375232268054231291134793762460637871279324378819 38533359809041132273867607787453012294417911504477754674305240059124044 34539300211520514925074441184624799491869097665516967359277837475057580 20491847749751091634820193268656091612946701305682912216359873206346501 42598357571488136035396967618255112414741952067122051109874940878684722 16227054032242518862820161479740883014764795654638469221295013624997521 98473387567535568329344950622097826805141495780519512875170913143065018 37662216942222278906237072134511470869122564099110975581229723528128874 03053691095257818202908316201329823392892324500902045647296018697874999 20634562994023688132981535956832405935997769986121211640316683000744236 63378284263645891973555848957172339312535422947108678254339441057481441 76487460271891016089273308979071882188401667272589323784415890014228351 05649723882311803795412909750706755024201214310554993418503482089069281 95615572005784333492681847166863358540123526041049740452126011153302182 69928416593506753653191196719544782903070012487865313234020529508526335 39411295911455100022266171613698329780452233640068267248122385816713610 87010369585205481319733864674354310151175968244604431168454967669874833 99287982520211602746481647806120954975561902387318665004489049852341783 72200348737319974223368564442777685004191437616639632602311154600318777 08508462316576557980274925052564330467031923823861950985803489683247997 67472005215268050318915551932025788426061626669626247375862108137218071 46614900048797041997449151792797211394077176051196770532535466559792922 88279242275068771484996290832709180445677598964610852404438155853150356 86419682712804065053412105128314417924421229867077959706012220783539856 37166352039984133733066378453065374465829728968133814682929592740556049 97247824388418914959563214576214835909181690137826744200560403496832921 43595383802676241403880692710582735755505565871509810870838824894422958 80010611404561773533396455228998025963781565619528243487410395448289946 33959386489256876612102324595395606813293998235167137623855053899740618 22336937771529560340697911432980190897260878937832029801778400452023577 89339735545302139106158835640996532374610376314478090979460230815953714 19491287785474232109983833918783359169726341538150525973101064555827807 77940695010331170100003029129489394981159784916828041560769099887875040 17589010418235420472052790902534682531500651103628303761892576728556089 23135432638933555354842807095260155471622240478140792639496232710848977 40316398605575858360576800875025710654970965170386173195083396491010673 99320305434009399225868962308407021798341806164170903590391478173649047 42768236673219171469653304713223275877926970751148623558797488774597430 53094873583715049858530972040200169993292286604282800317555200811866072 99555624799813940784608384429600719875327395546614924511267410932278069 80563229481659928270520714804664245992447044847352126821734898528472528 56928647600261504553159323481796028884454745091814400819576628784950975 41277941426893158423323104220277674134220259134247983746408807659230458 98907183153321639446143967665253940681801844239853859053704695935711976 55966742822076623399131520677072581030185186486255875820177694551218332 77694998518626556867183685415788957351935006965324511705452709739252778 22773515222654913225498479304827452018439115777632731076299933999397304 55194922239124524353759794616881857009653372640587489148424326746549738 05528351533445706011190985032454686563167818772182073654198505534207239 34362995411964693168911169284979883706334855812748565839380062778722175 83125600386353107814363739977002803146615382389051707088916330672987683 71037640676535751543450315162456976130873330701978727827976889275170961 03818753637972467203918607170711464373929376389753132293902002450105378 91732853220699489162244827908273357288986446929135657042772022444979497 41983210373540257735282312047597763149366558461184314620635258637108022 87685174438990771606809431009174805682861532126572079320455948668892988 84484036618019756649064959892829007345935331666984393889410839897801376 68070939028418337468947146844777457230788391300544224118178616799864518 64822215328300670516767715342789652432175051694709727395907850163768794 10878236440280834780149185972270681005533491965204221873212622325341594 71431202797339911747053812823916288525052630795689660316291415267941109 94016702276407575619414064627710336872458240088917632121723586646464907 15910988932147240972948822637604818587386468019325427770434120867752890 44296190708231641301406847095771241804242714848921074261553445899901340 68889899443163507728101052786026733086325104068201361869824316827043103 69643645945173175954471408788917840816698374309188644185335699431604035 33631292016149955514069994597205412392038589235554187602975761009278167 30562079965748362895411069015319876493654516294706881959445637663625629 82347853971707548710200626581122237108420828675184616539339574548423476 02326053334780332381135076116335120855592309275164552632253271137634537 28561617196019972482697861256690108951281123371630573354400484196800205 77844836206631290427075786900730104869861129869773283273761664764919272 59913099894784930279651463231800743230450304228283556459245908285520517 66402798093411988587913356683052833424753978357072267597364532438553971 57433855586843109570037220685641329378951442056397919801221935567170525 30624269567169988719133363722416991638350500141138465877694883093695784 20541755382917766387915141253605582589152033669464996093832448891110985 51580740180330040745779854878037247567468892064510101823178330848320460 46509210315095090143168475554611894524179473373557187751758228057639311 33466424307250986696340778177163070425754552356327835737830123966029367 41626809020133873763140594486895502511802330655355205681664288790672150 16641780317291396983760304009694200151765745776538720242174351892589643 40477355249236616160687010350753611506515619243912153119189873238516147 43104965026540718154387927063292596978713791302479341298280151640826897 08649753713207225488819270066956275996169960066097921116896250088286027 06949806341584778757282254415100890217070564381305288058697337893081516 89594583126742049772069961399100019632542344237462216169356270986230554 27116955563703898525759170567992645275234055145677650892478639666888680 22253014286037446436451084083015292741917210804986183546938855176855508 57556634846247661082128180165308186953041467227546723708186780452573226 97768139928519100700043126968974592797874522965061364615160832534019988 49057565738100906723885888235955488367226928294380059305336834708136033 33155970938656590234945311031837579752628178451192072699033981015339672 98942474525617991882619904305766109446206666255287089939950140222499290 60282933643319108138410994491361137464239786959888338328043807543811894 68955314850624635891916757116088888751456736254288875860512247602350715 02422416413419544037333021305551860527065610737541996362928800805438778 35216293252941070332509332749422366419636473117525399061992638337228786 60113258660417834957005079895221250857724895450360802032653267671571400 39174950531019109246308012957724231883735596029996768953622773766554272 62786611462917985272456976105451639189372234517590088664046332219409809 52619015230878160179168259715633490619343399951069866744641378342114402 61281716397039612448589214219649478903470294496018884019294874588915677 55090550939504370150394724808760227358971151480380074797985284843675651 29788693788925526476368034322781979026174787778321096819088134921292817 82584404027936028583489465381705630013398214929453466052952073425136484 29504531878061200055576915272388651798722633546763531880481337104803157 56587421796762567160870134691848124584801514420118218994253057665670600 44431123601830537906170712040066066669928971300763210897858302460643344 12309891464329120197198761306316780566934571608219193246298348385768856 03226622420182860663684574635948201973322762264198445925772444244979851 45101539602235722004203987081866694825334762975143780161101626130874570 77709152507804898568024042697362644068358498305302779528726029586337657 19911619486131607391227449669184339904377148354614944406188391540432793 5209589631280001
 
Logiclrd, I am interested in the logic and methods you used to generate the long form of 13[sup]12000[/sup] using qbasic. I remember a discussion of ways to add huge numbers using QB (thread314-22099). The process was horribly inefficient and the discussion died, I believe, when the machines started crashing.

Forgive this post for being slightly off-topic but I think a smattering of code and commentary could help to clarify both threads.
VCA.gif
 
Well, actually, I didn't generate it in QuickBASIC, simply because dealing with huge arrays in QB would have sort of distracted me from the actual calculations. However, the mechanics of doing it in QB aren't really that different from the way I did it (which is in C#). You can define multiplication using addition. I usually precalculate the multiples of the larger number and then use the smaller number to determine which multiples to add with which magnitudes. However, in QB, I don't think that would be feasible, so I would simply multiply them out and keep track of the carry as I went along (slightly slower). In C#, it was trivial for me to precalculate all the exponent levels as described in the previous post, but in QuickBASIC, again, storing these is infeasible, so I would simply discard the previous x[sup]2[sup]n[/sup][/sup] after calculating x[sup]2[sup]n+1[/sup][/sup]. Then, I would only need to store 3 "big" numbers in memory at any given time, per operation: for addition and multiplication, the two source operands and the destination array, and for exponentiation, the sum so far, the previous power-of-two-power, and the next power-of-two-power. This means a total of 9 arrays with maximum size of ~13500 indices. QuickBASIC can handle this. In total it is less than 128KB. :)

If QuickBASIC ever had troubles, it would be a simple matter to swap out to disk during intermediate operations. Modern disk caches would ensure that little time was wasted actually writing to and reading from the disk, so the overhead from doing the file operations would be minimal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top