diff options
author | jrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-03 03:26:46 +0000 |
---|---|---|
committer | jrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-03 03:26:46 +0000 |
commit | 1e1f6af9207b71845aa2f360e5d231825f317a09 (patch) | |
tree | c369805fe40cdaa9ee646cf98e44854be1538414 /third_party/lcov/example/methods/gauss.c | |
parent | 4acc19a6f31abef9608546d10f107240603ca57e (diff) | |
download | chromium_src-1e1f6af9207b71845aa2f360e5d231825f317a09.zip chromium_src-1e1f6af9207b71845aa2f360e5d231825f317a09.tar.gz chromium_src-1e1f6af9207b71845aa2f360e5d231825f317a09.tar.bz2 |
lcov-1.7 into third_party for code coverage on POSIX systems.
Non-lcov-1.7 files are
lcov/LICENCE
lcov/README.chromium
lcov/bin/mcov
Review URL: http://codereview.chromium.org/57083
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13066 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/lcov/example/methods/gauss.c')
-rw-r--r-- | third_party/lcov/example/methods/gauss.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/third_party/lcov/example/methods/gauss.c b/third_party/lcov/example/methods/gauss.c new file mode 100644 index 0000000..9da3ce5 --- /dev/null +++ b/third_party/lcov/example/methods/gauss.c @@ -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); +} |