Solution Review: Nested Loop with Multiplication (Basic)
This review provides a detailed analysis of how to solve the “Nested Loop with Multiplication” problem.
Solution #
The code for this challenge is reproduced below:
Press + to interact
Rust 1.57.0
fn main(){let n = 10;let mut sum = 0;let mut counter = 1;let pie = 3.14;while counter < n { // O(log(n))println!("{}", pie); // O(log(n))for _j in (1..n).step_by(2){ // O(log(n))*O(n)sum += 1; // O(log(n))*O(n)}counter *= 3; // O(log(n))}println!("{}", sum);}
Time complexity
Here’s a table that lists the running time line-by-line.
let n = 10; |
|
let mut sum = 0; |
|
let mut counter = 1; |
|
let pie = 3.14; |
|
while counter < n |
|
println!("{}",pie); |
|
let mut j=1; |
|
for _j in (1..n).step_by(2); |
|
sum+=1; |
Access this course and 1400+ top-rated courses and projects.