Runtime for while loop (Mathematics)
source link: https://www.codesd.com/item/runtime-for-while-loop-mathematics.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Runtime for while loop (Mathematics)
So I need a mathematical expression for the following loop, but I can't seem to grasp it. I am assuming I am just missing something simple.
while a <= b
a = a + a
end
Using an analysis, what would be the run time of this function?
The run time is dependent on the logarithm of b
. In other words, the time complexity is O(log N)
.
You can see this if you start a
at 1 and b
at 256. Each time through the loop, a
is doubled so that there are only nine iterations (would be eight if the condition was < b
).
Each doubling of the b
value will result in one extra iteration.
Of course, this is complexity analysis, the runtime depends on a host of other factors such as (almost certainly not an exhaustive list):
- how fast your machine is.
- what other things it has to do concurrently.
- initial value of
a
:a == 0
gives infinite run time,a == b + 1
gives you constant run time.
Related Articles
Overlapping time intervals WITHOUT for / while loops
Runtime Ignore While Loops?
I practice for / while loops. Ran in a string index error
Behavior of the C ++ iterator in for while loops
Trying to write for while loop, does not work
PHP for / while loop - Calculate dates based on start date and interval
How to open a text file containing numbers and print the sum. Must use a for / while loop?
Java while Loop related to a challenge presented in Java Programming for the Absolute Beginner
How to create a regular expression loop for while?
Conversion of an integer code from the while loop to For in Java
Create tables with for and while loops - Python 2
is for / while python loop is a generator
using lists in a for or while loop
Reassess the dependencies of a while loop
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK