From e23a43381ca6f3a8660449411fe043626435452a Mon Sep 17 00:00:00 2001 From: Jante Jomppanen Date: Wed, 25 Oct 2017 12:44:34 +0300 Subject: [PATCH 1/7] Update headers --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6c8e7f1..e776217 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ -#Math Tools +# Math Tools -##Overview +## Overview This project builds some basic math functions for the calculus of series. -##How to use it +## How to use it Just import the mathtools module. Each function is described in the code comments. -###Functions to be implemented: +### Functions to be implemented: * `isPrime`: tells if a number is prime. * `factorial`: Calculates the factorial of a number. * `fib`: Calculates the n value of the fibonacci sequence. * `geometric`: Calculates the sum of a geometric serie. * `arithmetic`: Calculates the sum of an arithmetic serie. -##How to test this -Run the test.py \ No newline at end of file +## How to test this +Run the test.py From 4529f140487c7b5505a1d42cc165e42f7eebaf05 Mon Sep 17 00:00:00 2001 From: patonkik Date: Wed, 25 Oct 2017 12:50:21 +0300 Subject: [PATCH 2/7] Added function to mathtools.py --- mathtools.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mathtools.py b/mathtools.py index 58081fa..8e97eb3 100644 --- a/mathtools.py +++ b/mathtools.py @@ -10,3 +10,9 @@ def isPrime(n): if n % i == 0: return False return True +def factorial(n): + '''Returns the factorial of a number''' + if n == 0: + return 1 + else: + return n * factorial(n-1) \ No newline at end of file From 86b3ccea5504d6025f3ac8d4fd3efe0352ce3049 Mon Sep 17 00:00:00 2001 From: patonkik Date: Wed, 25 Oct 2017 12:51:44 +0300 Subject: [PATCH 3/7] Removed test.py --- test.py | 62 --------------------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 test.py diff --git a/test.py b/test.py deleted file mode 100644 index ff90060..0000000 --- a/test.py +++ /dev/null @@ -1,62 +0,0 @@ -import mathtools - -def test_isPrime(): - fails = 0 - print "Testing isPrime" - primes = [2, 3, 5, 7, 983, 991, 997] - not_primes = [4, 6, 8, 9, 993, 994, 995, 999] - for prime in primes: - if mathtools.isPrime(prime): - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - for not_prime in not_primes: - if mathtools.isPrime(not_prime): - fails += 1 - print '- ', - else: - print '+ ', # Pass the test - - if not fails: - print "TEST OK" - else: - print ("FOUND ", fails, " ERRORS") - - -def test_factorial(): - print "Testing factorial " - fails = 0 - factorials = [(5, 120), (10, 3628800), (7, 5040), (12, 479001600)] - for f in factorials: - if mathtools.factorial(f[0]) == f[1]: - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - if not fails: - print "TEST OK" - else: - print "FOUND ", fails, " ERRORS" - -def test_fib(): - print "Testing fib " - fails = 0 - fib = [(1, 1), (0, 0), (3, 2), (4, 3), (6, 8), (7, 13), (20, 6765)] - for f in fib: - if mathtools.fib(f[0]) == f[1]: - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - if not fails: - print "TEST OK" - else: - print "FOUND ", fails, " ERRORS" - -functions = ('isPrime', 'factorial', 'fib') -for f in functions: - if hasattr(mathtools, f): - globals()['test_'+f]() - else: - print "*", f, "IS NOT DEFINED" \ No newline at end of file From cdf50ec266f93533d02acac09d97566a968c91ee Mon Sep 17 00:00:00 2001 From: patonkik Date: Wed, 25 Oct 2017 13:15:24 +0300 Subject: [PATCH 4/7] Added function fib to mathtools.py --- mathtools.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mathtools.py b/mathtools.py index 8e97eb3..51fbca9 100644 --- a/mathtools.py +++ b/mathtools.py @@ -15,4 +15,12 @@ def factorial(n): if n == 0: return 1 else: - return n * factorial(n-1) \ No newline at end of file + return n * factorial(n-1) +def fib(n): + ''' Calculates the n value of the fibonacci sequence''' + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1)+fib(n-2) \ No newline at end of file From 5a95429af53344c2d5f2907d5889459546044178 Mon Sep 17 00:00:00 2001 From: patonkik Date: Wed, 25 Oct 2017 13:20:31 +0300 Subject: [PATCH 5/7] Added function arithmetic to mathtools.py --- mathtools.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mathtools.py b/mathtools.py index 8e97eb3..916dd35 100644 --- a/mathtools.py +++ b/mathtools.py @@ -15,4 +15,12 @@ def factorial(n): if n == 0: return 1 else: - return n * factorial(n-1) \ No newline at end of file + return n * factorial(n-1) +def arithmetic(a, difference, n): + '''Calculates the sum of a arithmetic serie of n elements. + An arithmetic sequence is of the form: a, a+d, a+2d, a+3d,... + n is the number of elements in the sequence.''' + #Get the arithmetic sequence + sequence = [a+difference*x for x in range(n)] + #Calculates its sum + return sum(sequence) \ No newline at end of file From a75fb97853178f20b04239a1570d5667b3fb3373 Mon Sep 17 00:00:00 2001 From: patonkik Date: Wed, 25 Oct 2017 13:31:51 +0300 Subject: [PATCH 6/7] Added function geometric to mathtools.py --- mathtools.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mathtools.py b/mathtools.py index 7e59a28..494b908 100644 --- a/mathtools.py +++ b/mathtools.py @@ -32,3 +32,11 @@ def fib(n): return 1 else: return fib(n-1)+fib(n-2) +def geometric(a, ratio, n): + '''Calculates the sum of a geometric serie of n elements. + A geometric sequence is of the form: a, a*r, a*r*r, a*r*r*r,... + n is the number of elements in the sequence.''' + #Get the geometric sequence + sequence = [a*(ratio**x) for x in range(n)] + #Calculates its sum + return sum(sequence) \ No newline at end of file From dd18e19459fa7cf0f3e83c31c67d9d227163e197 Mon Sep 17 00:00:00 2001 From: Valtteri Kuosmanen Date: Wed, 25 Oct 2017 13:34:08 +0300 Subject: [PATCH 7/7] Muutettu --- mathtools.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mathtools.py b/mathtools.py index 7e59a28..0aae8a8 100644 --- a/mathtools.py +++ b/mathtools.py @@ -32,3 +32,9 @@ def fib(n): return 1 else: return fib(n-1)+fib(n-2) +def geometric(a, ratio, n): + '''Calculates the sum of a geometric serie of n elements. + A geometric sequence is of the form: a, a*r, a*r*r, a*r*r*r,... + n is the number of elements in the sequence.''' + #Use the sum formula: + return a*(1-ratio**n)/(1-ratio)