big integer adder Fibonacci Factorial complete source code c++

2
20



replaces previous source code videos. Includes latest vers. of xBigUint with enhanced Exception tracer messages, Fib , Fac , system of 3 Eqs. interface to autoDelete and autoDelete.

speed boost for both operator*=() function()’s

place n / 2 into each operator*=()
to re-place the “two line” brute-force for() Loop ,
be sure to // comment out the 2 line for() Loop code .
just after the Left Shift and zero .
17 to 20 % speed boost for *=
Make 3 static’s at the top of each function() ,…..

static LHS_ORG_ShiftTemp ;
static odd ;
static count ;

// START n / 2
if( MSD_RHS Greater Then 1 )
{

// initialize static’s before each re-use
odd = YES ;
count = MSD_RHS / 2 ; // floor
if( ( count + count ) == MSD_RHS )
odd = NO ;

LHS_ORG_ShiftTemp = LHS_ORG_Temp ;
for( xx = 1 ; xx Less Then count ; xx++ )
LHS_ORG_ShiftTemp = LHS_ORG_ShiftTemp + LHS_ORG_Temp ; // add one set ( half count of RHS ) / 2

LHS_ORG_ShiftTemp = LHS_ORG_ShiftTemp + LHS_ORG_ShiftTemp ; // double it , just one add+ to double it

if( odd )
LHS_ORG_ShiftTemp = LHS_ORG_ShiftTemp + LHS_ORG_Temp ; // add one more LHS( ORG ) when n is odd

*this = LHS_ORG_ShiftTemp + *this ;

}
else
{
if( 1 == MSD_RHS ) // also change the lower n / 2 to look like
*this = LHS_ORG_Temp + *this ; // as zero does nothing ,
// no for() Loop needed here .
}
// END n / 2

code re-use:

// place inside of operator*=( xUint_64 ) we could do just 3 code lines , at the very start of the try block .

static xBigUint RHS_as_xBigUint ;

// initialize static , before each re-use
RHS_as_xBigUint = ( xBigUint ) RHS ;

return( (*this).operator*=( RHS_as_xBigUint ) ) ;
// LHS RHS

// that’s it .
// code re-use really makes operator*=( xUint_64 ) small .
// besure to REM-out the remaining try block.

xBigUint 2 things:
1.
inside of both operator*=()’s , and operator+() 3 places,
REM-out the catch( bad_alloc ) blocks , as only the constructors catch bad_alloc’s

2.
in main() for big Fibonacci and big Factorial for() Loop’s
catch( xBigUintEx e ) include these two lines ,.. just after:

isNormalMsg_OK = NO ;

if( 0 != e.whatQue( 2 ) ) // bad_alloc occured via local static throw e ; // allocation ,
// during for() Loop try block ,
// the invalid numerical result won’t be shown .
// constructor throws should be “Poked” too .
// the que allows complex messages and we can poke the que
// while in main() to logically handle a particular error codemsg .

Nguồn:https://dothihoa.com/

2 COMMENTS

  1. comment was deleted . Here is my long reply to that , so as not to waste it :

    ​ KING MAGNUS fun and games , learning project , curiosity to make the
    huge adder . pi ? that is a tough one . Wanted to go for the record of
    most decimals of pi , but pi ,…. oh that pi . No pi .
    So , + – * , and some powers , Fibinacci , Factorial , Golden Ratio crunch .
    More interfaces of C++ . Not just the usual class interface to public methods()
    but also for interface hiding , making better Objects , and Overriding every
    last operator of c++ . All to make the Big integer adder . Involved a lot of stuff .
    see page 486 "Proxy Class" for the Interface encapsulation , which is used to
    make a .Lib that you want to develop with or to redistribute , You want to Hide
    your Source code behind an external interface . ( an extra level of Pointing )
    Eventually , I used the Proxy Class technique with .Lib files ,
    so I could use my own objects , from multiple .Lib files ,
    just like using someone else's SDK set of libraries .
    The .Libs contained all of the core Objects of the Big Integer adder .
    overall , it became a really big c++ learning experience .
    I put Fib , Factorial , Golden Ratio , Power functions()
    in a sperate Extern file . So I had to learn how to use "Extern"

    pi was one of the goals , but difficult to attain .
    it also made me implement 10's complement subtraction .
    ( I had to bust 10's comp. too , cause the books have insufficient info )
    lots of paper work , pseudo code , before typing the source .

    it also made me think of stupid stuff like : 3.1415 the decimal number
    mantissa .1415 aaahh , even though it is less then 1 , the mantissa
    is all Integers too ! It's all Integers ! That might sound silly , but when
    manually building the mantissa , it had be an Integer for each digit
    location , what else can it be ? all righty then .

    One use ?
    if the math equation is simple enough to make with the Big Integer Adder ,
    and someone needs more mantissa resolution then that of a hand held
    calculator . Out to 1,000 .decimal places is pretty fast . 1 to 10 million
    decimal places could take a few days to finish .

    lotto numbers ?

  2. motivation originally came from book ""C++ How to Program" Third Edition ISBN:
    0-13-089571-7 Prentice Hall publisher . see page 572 HugeInt .

    The book's "snakery" worked , but they coded to always Zero out the digits . Their code only had good performance for less then 100 digits .

    I was always trying to improve speed performance . Snakery being performance improvements . Usually faster code becomes more convoluted ( snakery ) . Simpler code

    is usually easy to understand , but may have long winded iterations which take much longer to run .

    Faster code is usually larger source code ,…more decision in the code that avoid excessive iterations ( smart code ) . ( that is a common theme with optimizing code ) ,…. the source code gets bigger while the executable code runs faster .

    P.S.

    Faster code actually does Less ! yes . The faster running code avoids excessive

    iterations , thus the faster code actually does Less , but the exact same result is achieved .

    So , bust an algorithm and make your own snakery optimized code

LEAVE A REPLY

Please enter your comment!
Please enter your name here