Given the pseudocode :
e = 1
sum = 1
for i=2 upto n
e *= 10
sum += i * e
Doing exponential suing multiplication since it’s much faster. Assuming n can be 10 ^ 1000 or bigger how would you go about getting the big O notation for something like this.
Of course it’s going to be at a minimum O(n), but how much does the multiplication and addition add to the complexity. Again, with large numbers.
I’m currently doing this in Ruby. I presume each language has a different way of implementing math operations so a general solution is fine.
For this to work with n as large as 10^1000 you would definitely need to use a BigNumber implementation for the arithmetic operations.
Since you’re using a software implementation for the arithmetic operations that means that their time complexity will likely depend on the size (number of bits) of the numbers you’d be working with. In case of addition, the complexity would likely be linear and for multiplication it will be quadratic or in any case supra-linear.
In that case all you have to do is plug that complexity into your algorithm above to obtain the combined complexity.
For instance, if you have:
Assuming the multiplication is quadratic in the size (# of bits) of n then
O(mult) = (log n) ^ 2. Then the big-Oh complexity of the for-loop will be:In your example, the for loop contains three potentially “expensive” operations:
In the above, the expensive operations are:
mult1: e*10,mult2: i * e, andadd: sum + (result of i*e)Their combined complexity is going to be
O(mult1 + mult2 + sum), which is going to take the value of the largest of three. In this case, definitelymult2So if you can obtain a bound on the multiplication operation then the total is complexity is going to be, as stated above:
n * O(mult), which again, assuming a quadratic implementation in the size of numbers, is going to translate to something like: O(n * log(n))As far as estimating the run-time characteristics of arithemetic operations, here’s a nice table from Wikipedia of different algorithms for basic arithmetic operations.