-
3 Comments
Okay, so we can definitely do better than a for loop...
@Micah Beckman no, this solution is not an "efficient" answer even though it scores high... one should use sum(1:n) to replace a for loop sum.
While people are tricking the scoring mechanism using regexp, sum(1:n) isn't the most efficient.
It's helpful to recognize that this is an arithmetic series starting at 1 and ending at n with an increments of 1. The sum of such an arithmetic series is n*(n+1)/2. You can see the difference if you use 'tic' 'toc' to time sum(1:n) and n*(n+1)/2 for very large n.
Suggested Problems
-
Create a square matrix of multiples
482 Solvers
-
583 Solvers
-
Simple equation: Annual salary
4206 Solvers
-
677 Solvers
-
476 Solvers
More from this Author96
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!