From 84320a350af6ea14c6e1540e5e456783cea1566c Mon Sep 17 00:00:00 2001 From: Emil-Kiv Date: Fri, 25 Sep 2020 13:47:29 +0300 Subject: [PATCH 1/8] muokattiin mathtools.py --- mathtools.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mathtools.py b/mathtools.py index 58081fa..1d42b8d 100644 --- a/mathtools.py +++ b/mathtools.py @@ -10,3 +10,10 @@ 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 6d30a12e3c614bc2b48dfa30bb00dc1d16f128ad Mon Sep 17 00:00:00 2001 From: Emil-Kiv Date: Fri, 25 Sep 2020 13:49:28 +0300 Subject: [PATCH 2/8] poistettiin 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 89a3a500b1237b4ddbeb87ecd69edca8b521a068 Mon Sep 17 00:00:00 2001 From: Opiumkebab Date: Fri, 25 Sep 2020 14:23:40 +0300 Subject: [PATCH 3/8] muutettu mathtools.py --- mathtools.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mathtools.py b/mathtools.py index 58081fa..77b5087 100644 --- a/mathtools.py +++ b/mathtools.py @@ -10,3 +10,12 @@ def isPrime(n): if n % i == 0: return False return True + +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 c264758b8d1431e1b5d5b80686ce739b9e5ab0af Mon Sep 17 00:00:00 2001 From: Opiumkebab Date: Fri, 25 Sep 2020 14:28:44 +0300 Subject: [PATCH 4/8] taas muutettu mathtools.py --- mathtools.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mathtools.py b/mathtools.py index 58081fa..112b15c 100644 --- a/mathtools.py +++ b/mathtools.py @@ -10,3 +10,12 @@ def isPrime(n): if n % i == 0: return False return True + +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 de4f1d9823c063985d4d95867e7ca4af8b5e6321 Mon Sep 17 00:00:00 2001 From: Opiumkebab Date: Fri, 25 Sep 2020 14:53:07 +0300 Subject: [PATCH 5/8] paras muutos ikina --- mathtools.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mathtools.py b/mathtools.py index 6ff4604..5aeffe1 100644 --- a/mathtools.py +++ b/mathtools.py @@ -30,3 +30,11 @@ def fib(n): 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) From 1879a8a3bddcb62b11a8c31978f1f5447a7d95c6 Mon Sep 17 00:00:00 2001 From: Emil-Kiv Date: Fri, 25 Sep 2020 14:55:22 +0300 Subject: [PATCH 6/8] jees --- mathtools.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mathtools.py b/mathtools.py index 8d78841..e42cf0d 100644 --- a/mathtools.py +++ b/mathtools.py @@ -36,4 +36,9 @@ def fib(n): 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) From 4bf922675eddd83e878a9b1c93f38485cecd1665 Mon Sep 17 00:00:00 2001 From: Emil-Kiv Date: Fri, 25 Sep 2020 15:01:40 +0300 Subject: [PATCH 7/8] parempi math --- mathtools.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/mathtools.py b/mathtools.py index f071036..e42cf0d 100644 --- a/mathtools.py +++ b/mathtools.py @@ -40,11 +40,5 @@ 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) - - #Get the geometric sequence - sequence = [a*(ratio**x) for x in range(n)] - #Calculates its sum - return sum(sequence) From 4cd6598284628444bec5bf0ef394de4081c3fbc9 Mon Sep 17 00:00:00 2001 From: Opiumkebab Date: Fri, 25 Sep 2020 15:09:58 +0300 Subject: [PATCH 8/8] we must go back --- test.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..ff90060 --- /dev/null +++ b/test.py @@ -0,0 +1,62 @@ +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