13

Runtime for while loop (Mathematics)

 3 years ago
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.
neoserver,ios ssh client

Runtime for while loop (Mathematics)

advertisements

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

The best way to ask my question is via a clear example. Consider 2 timelines (e.g. time in seconds) A and B where the intervals for each timeline are: intervals_a = 0 1 1 4 4 7 7 9 intervals_b = 0 2 2 3 3 5 5 8 Notice that the first a-interval overla

Runtime Ignore While Loops?

I am new to the forums so first of all I'd like to say "Hi"! I'm new to Java programming and am trying to make a simple payroll calculating program with three while loops. The first while loop keeps the program going until the user enters the se

I practice for / while loops. Ran in a string index error

def onlyLetters(seq): seq = seq.lower() letter = ('a', 'b', 'c', 'z') for i in range(len(seq)): if seq[i] != letter: seq = seq[:i] + seq[i+1:] return seq I'm trying to make a for loop that iterates over all elements of the string seq, and check if it

Behavior of the C ++ iterator in for while loops

I do not understand why iterating through a container with a for loop produces different results than iterating through it with a while loop. The following MWE illustrates this with a vector and a set of 5 integers. #include <iostream> #include <

Trying to write for while loop, does not work

This is my first question, so I am sorry if it needs retitling or moving to another section. I am trying to write a loop that will append numbers 1-4 to every string in list1 and put those new strings in list2. Here is what I'm doing so far: number =

PHP for / while loop - Calculate dates based on start date and interval

I've got two fields in my database, a start_date and an interval. Based on these values, I want to calculate other dates. For example: To keep it simple, the start_date is january 1st. The interval is 3. This means that each time the date month shoul

How to open a text file containing numbers and print the sum. Must use a for / while loop?

Title says all, I want to have a script that opens a text file, finds the numbers, (in number form not like "one" "two" etc) and prints/returns the sum of those numbers. This is all I have: def count_num(filename): myfile=open(filename

Java while Loop related to a challenge presented in Java Programming for the Absolute Beginner

I am attempting to learn Java Programming on my own (without classes/teachers/tutors/etc.) so this is not a homework assignment. My question has been alluded to in Java Challenge for beginners: Generate random numbers in a while loop, however, I have

How to create a regular expression loop for while?

I am trying to build a regular expresion for while loop to include in my program. This is what I have come up with so far: https://regex101.com/r/aL0zC7/11 /^ *while +\( *([a-zA-Z][a-zA-Z0-9_]*) *([=<>]) *([a-zA-Z][a-zA-Z0-9_]*|[0-9]+) *\) *$/ But t

Conversion of an integer code from the while loop to For in Java

I have tried converting a code from while loop to For loop but Im not getting the desired output. The For Loop code is: public static void diamond1() { System.out.println("Diamond Height: " + DIAMOND_SIZE); System.out.println("Output for: F

Create tables with for and while loops - Python 2

I have the following program which successfully creates a 10x20 array filled with zeros: array2 = [] array2=[[0 for j in range(10)] for i in range(20)] print array2 I then tried to do the same with a for loop and a while loop: for j in range(20): for

is for / while python loop is a generator

In an interview , the interviewer asked me for some of generators being used in Python. I know a generator is like a function which yield values instead of return. so any one tell me is for/while loop is an example of generator.Short answer: No, but

using lists in a for or while loop

so there is something I don't understand about these two loops If I have a list called numbers to find the largest number using a for loop I can: for number in numbers: if number > largest: largest = number but why doesn't: if number[index] > larges

Reassess the dependencies of a while loop

I am trying to understand how while loops work in tensorflow. In particular I have a variable, x say, that I update in the while loop, and then I have some values that depends on x, but when running the while loop the values does not seem to be updat

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK