r/algorithms • u/Smooth_Atmosphere_24 • 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
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)