Search⌘ K
AI Features

Solution: Nested Loop With Multiplication (Basic)

Explore how to analyze the time complexity of nested loops involving multiplication in JavaScript. Understand step-by-step execution counts and how to derive Big O complexity using logarithmic functions for efficient algorithm design.

We'll cover the following...

Solution

Node.js
// Initializations
const n = 10;
const pie = 3.14;
let sum = 0;
var i = 1;
while (i < n) {
console.log(pie);
for (var j = 1; j < n; j += 2) {
sum = sum + 1;
}
i *= 3;
}
console.log(sum)

Time Complexity

The outer loop in this problem runs log3(n)log_3(n) ...