Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1522)

Unified Diff: third_party/lcov/example/methods/gauss.c

Issue 57083: lcov-1.7 into third_party for code coverage on POSIX systems.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/lcov/example/iterate.h ('k') | third_party/lcov/example/methods/iterate.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/lcov/example/methods/gauss.c
===================================================================
--- third_party/lcov/example/methods/gauss.c (revision 0)
+++ third_party/lcov/example/methods/gauss.c (revision 0)
@@ -0,0 +1,48 @@
+/*
+ * methods/gauss.c
+ *
+ * Calculate the sum of a given range of integer numbers.
+ *
+ * Somewhat of a more subtle way of calculation - and it even has a story
+ * behind it:
+ *
+ * Supposedly during math classes in elementary school, the teacher of
+ * young mathematician Gauss gave the class an assignment to calculate the
+ * sum of all natural numbers between 1 and 100, hoping that this task would
+ * keep the kids occupied for some time. The story goes that Gauss had the
+ * result ready after only a few minutes. What he had written on his black
+ * board was something like this:
+ *
+ * 1 + 100 = 101
+ * 2 + 99 = 101
+ * 3 + 98 = 101
+ * .
+ * .
+ * 100 + 1 = 101
+ *
+ * s = (1/2) * 100 * 101 = 5050
+ *
+ * A more general form of this formula would be
+ *
+ * s = (1/2) * (max + min) * (max - min + 1)
+ *
+ * which is used in the piece of code below to implement the requested
+ * function in constant time, i.e. without dependencies on the size of the
+ * input parameters.
+ *
+ */
+
+#include "gauss.h"
+
+
+int gauss_get_sum (int min, int max)
+{
+ /* This algorithm doesn't work well with invalid range specifications
+ so we're intercepting them here. */
+ if (max < min)
+ {
+ return 0;
+ }
+
+ return (int) ((max + min) * (double) (max - min + 1) / 2);
+}
« no previous file with comments | « third_party/lcov/example/iterate.h ('k') | third_party/lcov/example/methods/iterate.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698