difference between Do Loop until and not operator in while loop? - vb.net

Dim n As Integer
Do Until n > 5
n = n + 10
Loop
Debug.WriteLine(n)
VS
Dim n As Integer
While Not n > 5
n = n + 10
End While
Debug.WriteLine(n)
output: 10 (in both)

The difference is that the Not is an extra operator. The result is the same.
You can do the same with the complement to the > operator, so that you don't need one operator more:
While n <= 5
n = n + 10
End While
You can also do the same with Do While:
Do While n <= 5
n = n + 10
Loop
The Do ... Loop construct is more flexible in that you can use While or Until at either end of the loop. As Do ... Loop can do anything that While can, it's kept mostly for backwards compatibility.

Related

What is the complexity of this function in terms of n?

f(n):
s = 1
m = n
while m >= 1:
for j in 1,2,3, .., m:
s = s + 1
m = m / 2
My attempt
How many times does the statement s = s + 1 run? Well, let's try a number or two for n and see what we get.
n = 32
m = n = 32
m = 32 => inner for loop, loops 32 times
m = 16 => -.- 16 times
m = 8 => -.- 8 times
m = 4 => -.- 4 times
m = 2 => -.- 2 times
m = 1 => -.- 1 time
In total 16+8+4+2+1 = 31 times for n = 32
Doing the same for n = 8 gives 15 times
Doing the same for n = 4 gives 7 times
Doing the same for n = 64 gives 127 times
Notice how it always seemingly is 2 * n - 1, I believe this to be no coincidence because what we're really observing is that the total amount of times the statement gets executed is equal to the geometric sum sum of 2^k from k=0 to k=log(n) which has a closed form solution 2n + 1 given that we're using base 2 logarithm.
As such, my guess is that the complexity is O(n).
Is this correct?
Well what you just observed is true indeed. We can prove it mathematically as well.
Inner loop execution for first iteration -> n
Inner loop execution for second iteration -> n/2
Inner loop execution for third iteration -> n/(2^2)
and so on....
Therfore total time is (n + (n/2) + (n/(2^2)) + ... + (n/(2^k))) where n/(2^k) should be equal to 1 which implies k = log(n). Taking n common from all terms will give us a GP And now using GP formulae in above series , total time will come out to be 1(1 - ((1/2)^k)) / (1 - 1/2). putting value of k = log(n), we will get total time as 2*(n-1).
Note -:
This gives us time complexity of O(n).
log is of base 2.

Why is the second line executed n (n + 1 / 2)

1. for i = 1 to n
2. for j = i + 1 to n
[...]
Why does my textbook say that the second line is executed n (n + 1 / 2) + 1 times?
I know that the first line will be executed n + 1 times because the + 1 will break the loop
The nested loop iteration count will be reduced by one each time ( see the picture below). Nevertheless , the whole code is in O(n^2). O(n*( (n+1)/2) = O( n*n/2+n/2) =O(n^2)
n=5
*****
****
***
**
*
The outer loop will result in n iterations of the inner loop. Each of these iterations will result in n-i loops. Since i ranges from i to n this thus results in a sum that looks like:
n + n-1 + n-2 + ... 1
This is a known sum. The sum of i with i from 1 to n is the same as:
n
---
\ n * (n + 1)
/ i = -----------
--- 2
i=1

is it possible to loop though 2 values using only 1 loop?

I have mutable variables x and y, and I can only use a while (something) loop. I want to loop through 0 .. 9 and 0 .. 9. I have made many attempts using different if statements and in different orders. Here's what I have so far.
open System
let mutable x = 0
let mutable y = 0
let n = 9
while x <> n && y <> n do
Console.SetCursorPosition(x, y)
printf "."
// ...
Console.Read() |> ignore
The normal way of doing this would be to use two nested loops - if you're iterating over a known number of items (rather than unboundedly, until some condition holds), then for loop is easier:
for x in 0 .. 9 do
for y in 0 .. 9 do
Console.SetCursorPosition(x, y)
printf "."
The nested loop iterates 10 times and the outer loop runs the nested one 10 times, so you get 100 executions of the nested body.
You could do this with just one loop if you iterate over 100 values, i.e. 0 .. 10*10-1 which is 0 .. 99. If you have numbers from 0 to 99, you can then calculate x and y by taking x=n/10 and y=n%10:
for n in 0 .. 10 * 10 - 1 do
let x = n / 10
let y = n % 10
Console.SetCursorPosition(20+x, y)
printf "."

Running time of nested while loops

Function f(n)
s = 0
i = 1
while i < 7n^1/2 do
j = i
while j > 5 do
s = s + i -j
j = j -2
end
i = 5i
end
return s
end f
I am trying to solve the running time for big theta with the code above. I have been looking all over the place for something to help me with an example, but everything is for loops or only one while loop. How would you go about this problem with nested while loops?
Let's break this down into two key points:
i starts from 1, and is self-multiplied by 5, until it is greater than or equal to 7 sqrt(n). This is an exponential increase with logarithmic number of steps. Thus we can change the code to the following equivalent:
m = floor(log(5, 7n^(1/2)))
k = 0
while k < m do
j = 5^k
// ... inner loop ...
end
For each iteration of the outer loop, j starts from i, and decreases in steps of 2, until it is less than or equal to 5. Note that in the first execution of the outer loop i = 1, and in the second i = 5, so the inner loop is not executed until the third iteration. The loop limit means that the final value of j is 7 if k is odd, and 6 if even (you can check this with pen and paper).
Combining the above steps, we arrive at:
First loop will do 7 * sqrt(n) iterations. Exponent 1/2 is the same as sqrt() of a number.
Second loop will run m - 2 times since first two values of i are 1 and 5 respectively, not passing the comparison.
i is getting an increment of 5i.
Take an example where n = 16:
i = 1, n = 16;
while( i < 7 * 4; i *= 5 )
//Do something
First value of i = 1. It runs 1 time. Inside loop will run 0 times.
Second value of i = 5. It runs 2 times. Inside loop will run 0 times.
Third value of i = 25. It runs 3 times. Inside loop will run 10 times.
Fourth value of i = 125. It stops.
Outer iterations are n iterations while inner iterations are m iterations, which gives O( 7sqrt(n) * (m - 2) )
IMO, is complex.

What is the order of growth of this function?

I'm new to algorithm and big 0. What is the order of growth of this function?
I do a println and f(10) runs 15 times. f(20) runs 31 times.
It looks to me like log(N)*N/2. So it is logarithmic or linearithmic?
static long f (long N) {
long sum = 0;
for (long i = 1; i < N; i *= 2)
for (long j = 0; j < i; j++)
sum++;
return sum;
}
The runtime is O(n). To see this, note that the inner loop runs 1 time on the first iteration, 2 times on the next iteration, 4 times on the next iteration, and more generally 2i times on the 2ith iteration. The outer loop stops after lg n iterations because it keeps doubling, so the total work done is
1 + 2 + 4 + 8 + ... + 2lg n
This is the sum of a geometric series and works out to 2lg n + 1 - 1 = 2 ยท 2lg n - 1 = 2n - 1 = O(n).
Hope this helps!
Inner loop j counts i times -> max is n
Outter loop counts from 0 to n, multiplying by 2 each time, so it's lgn times.
So total is o(nlgn)
Proceeding formally, you obtain:
O(2^lgn) should be the complexity.growth of exponential function is more than a linear funtion.Hence 2.2^lgn=O(2^lgn) instead of O(n)