Search⌘ K
AI Features

Solution Review: Big (O) of Nested Loop with Multiplication

Explore how nested loops with geometric progression impact algorithm time complexity. Learn to analyze and calculate the Big O notation for such loops, especially those involving logarithmic outer loops and multiplicative inner loops, to better evaluate algorithm efficiency in your C# code.

We'll cover the following...

Solution

C#
namespace Chapter_1
{
class Challenge_3
{
static void Main(string[] args)
{
int n = 10;
int sum = 0;
float pie = 3.14f;
int counter = 1;
while (counter < n) // O(log n)
{
Console.WriteLine(pie); // O(log n)
for (int j = 0; j < counter; j++)// 8n - 12
sum += 1;// (4n - 6)
counter *= 2;// O(log n)
}
Console.WriteLine(sum);
}
}
}

Time Complexity

The outer loop here runs log(n ...