0% found this document useful (0 votes)
2 views4 pages

Comparing Efficiency of Two Algorithms

The document compares the efficiency of two algorithms for calculating the sum of factorials: a nested loop approach and a single loop approach. The single loop is more efficient with a time complexity of O(n) compared to O(n²) for the nested loop, making it better for larger values of n. The single loop also simplifies implementation and reduces unnecessary calculations, while the nested loop suffers from poor scalability due to redundant calculations.

Uploaded by

Masila Mani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Comparing Efficiency of Two Algorithms

The document compares the efficiency of two algorithms for calculating the sum of factorials: a nested loop approach and a single loop approach. The single loop is more efficient with a time complexity of O(n) compared to O(n²) for the nested loop, making it better for larger values of n. The single loop also simplifies implementation and reduces unnecessary calculations, while the nested loop suffers from poor scalability due to redundant calculations.

Uploaded by

Masila Mani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Comparing Efficiency of Two

Algorithms for Sum of


Factorials
1. Using Nested Loops 2. Using a Single
Loop
for (int i = 1; i <= 5; i++)
{ int factorial = 1;
Comparis int factorial = 1; for (int i = 1; i <= 5; i+
+)
on for (int j = 1; j <= i; j++)
{
{
factorial *= j;
factorial *= i;

} sum += factorial;

sum += factorial; }
Practical Comparison
for n=5
}
Nested Loop: Single Loop:
• Outer loop runs n=5 • Single loop runs n=5
times. times.
• Inner loop executes • Total multiplications =
1+2+3+4+5=15 5.
iterations.
Efficiency Analysis

Metric Nested Loop Single Loop


Time Complexity O(n2) O(n)

The inner loop executes ii times The factorial is updated


Reasoning for each iteration of the outer incrementally in a single loop,
loop, leading to quadratic growth. avoiding repeated calculations.

Space Complexity O(1) O(1)

Both approaches use a constant


Same as the nested loop
Reasoning amount of memory, with variables
approach.
for the sum and factorial.

Number of Multiplications 1+2+3+...+n=n(n+1)/2 n

Each factorial calculation starts Each factorial builds upon the


Reasoning fresh, performing repeated previous one, avoiding
multiplications. redundancy.

Poor scaling due to redundant Scales better due to efficient


Performance for Large n
calculations. updates.
Advantages of the Single Loop Approach
• Lower Time Complexity: Reduces unnecessary
calculations.
• Better Scalability: Handles larger values of nn more
efficiently.
• Simpler Implementation: Fewer loops mean less code
Conclusion
complexity and better readability.
• The single loop algorithm is more efficient, especially
as n grows larger.
• The nested loop algorithm may be acceptable for small
n, but it performs poorly for larger inputs due to
redundant factorial calculations.

You might also like