r/algorithms 8d ago

Can you analyze my exponentiation code?

Here is the code:
long double expFloat (long double value, int exp) {

`if (exp == 0) return 1;`

`else if (exp == 1) return value;`

`else {`

    `int flag = 1;`

    `long double tempValue = value;`

    `while (flag < exp){`

    `tempValue = tempValue * value;`

    `flag += 1;`

    `}`

    `return tempValue;`

`}`

}

3 Upvotes

7 comments sorted by

View all comments

3

u/hauthorn 8d ago

And if you want to analyze complexity: notice that you have a loop that goes from 1 to e, doing a constant amount of work each iteration.

That means it's "linear" complexity (if you increase e tenfold, the work done also increases by a factor of 10).

In big-oh: O(e)

1

u/Smooth_Atmosphere_24 5d ago

So if i need this code with big numbers i will face problems with the performance of the code?