diff --git a/printNumbers/functions/gaussian_summation.py b/printNumbers/functions/gaussian_summation.py
new file mode 100644
index 0000000..7b67fde
--- /dev/null
+++ b/printNumbers/functions/gaussian_summation.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+#
+# fibonacci.py
+#
+# This file is part of printNumbers.
+#
+# Copyright (C) 2022 C. Scherer, Simulation & Datalab Neuroscience, JSC, FZ Juelich
+#
+# printNumbers is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# printNumbers is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with printNumbers. If not, see .
+
+def GaussianSummation(n):
+ '''
+ Helper function.
+ '''
+ if n == 0:
+ return n
+ else:
+ return GaussianSummation(n - 1) + n
diff --git a/printNumbers/parameters.py b/printNumbers/parameters.py
index aebe0c3..0c57734 100755
--- a/printNumbers/parameters.py
+++ b/printNumbers/parameters.py
@@ -29,6 +29,7 @@
CONST_MAX_OPERAND_VAL = 20
CONST_FUNC_CODE_FIBONACCI = 0
CONST_FUNC_CODE_FACTORIAL = 1
+CONST_FUNC_CODE_GAUSSIAN_SUMMATION = 2
class Parameters(object):
@@ -43,6 +44,8 @@ def __setParameters(self, cmdLineArgs):
self.functionIndex = CONST_FUNC_CODE_FIBONACCI
elif cmdLineArgs['--factorial']:
self.functionIndex = CONST_FUNC_CODE_FACTORIAL
+ elif cmdLineArgs['--gaussiansummation']:
+ self.functionIndex = CONST_FUNC_CODE_GAUSSIAN_SUMMATION
@property
def operand(self):
diff --git a/printNumbers/printNumbers.py b/printNumbers/printNumbers.py
index 9f9ef0e..37dbd2a 100755
--- a/printNumbers/printNumbers.py
+++ b/printNumbers/printNumbers.py
@@ -27,24 +27,27 @@
"""
Usage:
printNumbers.py -h --help
- printNumbers.py [--fibonacci|--factorial]
+ printNumbers.py [--fibonacci|--factorial|--gaussiansummation]
Options:
- -h --help Print usage.
- --fibonacci Print the fibonacci sequence.
- --factorial Print the factorial.
+ -h --help Print usage.
+ --fibonacci Print the fibonacci sequence.
+ --factorial Print the factorial.
+ --gaussiansummation Print the sum of the first n integers
"""
from docopt import docopt
from parameters import *
from functions.fibonacci import *
from functions.factorial import *
+from functions.gaussian_summation import *
#
# FUNCTION TABLE
#
-functionTable = { CONST_FUNC_CODE_FIBONACCI : FibonacciSequence,
+functionTable = { CONST_FUNC_CODE_FIBONACCI : FibonacciRecursion,
CONST_FUNC_CODE_FACTORIAL : Factorial,
+ CONST_FUNC_CODE_GAUSSIAN_SUMMATION : GaussianSummation,
}
#
@@ -67,3 +70,5 @@
print('fib(' + str(params.operand) + ') =', result)
elif params.functionIndex == CONST_FUNC_CODE_FACTORIAL:
print(str(params.operand) + '! =', str(result))
+ elif params.functionIndex == CONST_FUNC_CODE_GAUSSIAN_SUMMATION:
+ print('gaussian_summation(' + str(params.operand) + ') =', result)
diff --git a/printNumbers/unittests/runTestSuite.py b/printNumbers/unittests/runTestSuite.py
index f3dca23..c79504e 100755
--- a/printNumbers/unittests/runTestSuite.py
+++ b/printNumbers/unittests/runTestSuite.py
@@ -27,6 +27,7 @@
import test_factorial
import test_fibonacci
+import test_gaussian_summation
def suite():
@@ -34,6 +35,7 @@ def suite():
suite.addTest(test_factorial.suite())
suite.addTest(test_fibonacci.suite())
+ suite.addTest(test_gaussian_summation.suite())
return suite
diff --git a/printNumbers/unittests/test_gaussian_summation.py b/printNumbers/unittests/test_gaussian_summation.py
new file mode 100644
index 0000000..eab69c2
--- /dev/null
+++ b/printNumbers/unittests/test_gaussian_summation.py
@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*-
+#
+# test_factorial.py
+#
+# This file is part of PrintNumbers.
+#
+# Copyright (C) 2022 C. Scherer, Simulation & Datalab Neuroscience, JSC, FZ Juelich
+#
+# PrintNumbers is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# PrintNumbers is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with PrintNumbers. If not, see .
+
+#
+# Unit tests: 'gaussian_summation'.
+#
+
+import sys, os
+sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+
+import unittest
+from functions.gaussian_summation import *
+
+class TestGaussianSummation(unittest.TestCase):
+
+ def test_value_0(self):
+ self.assertEqual(GaussianSummation(0), 0)
+
+ def test_value_1(self):
+ self.assertEqual(GaussianSummation(1), 1)
+
+ def test_value_2(self):
+ self.assertEqual(GaussianSummation(2), 3)
+
+ def test_value_20(self):
+ self.assertEqual(GaussianSummation(20), 210)
+
+
+def suite():
+ suite = unittest.makeSuite(TestGaussianSummation, 'test')
+ return suite
+
+def run():
+ runner = unittest.TextTestRunner(verbosity = 2)
+ runner.run(suite())
+
+if __name__ == "__main__":
+ run()
\ No newline at end of file