From cc21ea1f420b8163d9f505adaeb2a15f8631a460 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 04:45:23 +0530 Subject: [PATCH 01/16] Added Python Program to Check Perfet Number --- maths/perfect_number.py | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 maths/perfect_number.py diff --git a/maths/perfect_number.py b/maths/perfect_number.py new file mode 100644 index 000000000000..0c52d04c40a8 --- /dev/null +++ b/maths/perfect_number.py @@ -0,0 +1,44 @@ +""" + == Perfect Number == + In number theory, a perfect number is a + positive integer that is equal to the sum + of its positive divisors, excluding the + number itself. + >>> For 6 ==> divisors[1, 2, 3, 6] + Excluding 6 sum(divisors) = 1 + 2 + 3 = 6 + So, 6 is a Perfect Number + Other examples of Perfect Numbers: 28, 486, ... + +""" + + +def perfect(number: int) -> bool: + divisors = [] + for i in range(1, ((number // 2) + 1)): + """ + starting from 1 as divion by 0 + will raise error. + A number at most can be divisible + by the half of the number except + the number itself + >>> 6 at most can be divisible by 3 + except 6 itself + """ + + if (number % i) == 0: + divisors.append(i) + + if sum(divisors) == number: + return True + else: + return False + + +if __name__ == "__main__": + print("Program to check whether a number is a Perfect number or not.......") + number = int(input("Enter number: ")) + check = perfect(number) + if check: + print("{} is a Perfect Number.".format(number)) + else: + print("{} is not a Perfect Number.".format(number)) From fb23a6c317ac94baa2fb8db54bb733f5759fac33 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 04:57:50 +0530 Subject: [PATCH 02/16] CodeSpell Error Fix - 1 --- maths/perfect_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index 0c52d04c40a8..449238b8c306 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -16,7 +16,7 @@ def perfect(number: int) -> bool: divisors = [] for i in range(1, ((number // 2) + 1)): """ - starting from 1 as divion by 0 + starting from 1 as division by 0 will raise error. A number at most can be divisible by the half of the number except From 0c6adf13244ddc93eaa3f8419eca1f80ebaeaec7 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 12:57:10 +0530 Subject: [PATCH 03/16] Build Error Fix - 1 --- maths/perfect_number.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index 449238b8c306..f7c9c7b07a7e 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -1,14 +1,15 @@ """ - == Perfect Number == - In number theory, a perfect number is a - positive integer that is equal to the sum - of its positive divisors, excluding the - number itself. - >>> For 6 ==> divisors[1, 2, 3, 6] - Excluding 6 sum(divisors) = 1 + 2 + 3 = 6 - So, 6 is a Perfect Number - Other examples of Perfect Numbers: 28, 486, ... +== Perfect Number == +In number theory, a perfect number is a positive integer +that is equal to the sum of its positive divisors, excluding +the number itself. +For 6 ==> divisors[1, 2, 3, 6] +Excluding 6 sum(divisors) = 1 + 2 + 3 = 6 +So, 6 is a Perfect Number +Other examples of Perfect Numbers: 28, 486, ... + +https://en.wikipedia.org/wiki/Perfect_number """ @@ -21,8 +22,17 @@ def perfect(number: int) -> bool: A number at most can be divisible by the half of the number except the number itself - >>> 6 at most can be divisible by 3 - except 6 itself + 6 at most can be divisible by 3 + except 6 itself + """ + + """ + >>> perfect(27) + False + >>> perfect(28) + True + >>> perfect(29) + False """ if (number % i) == 0: @@ -37,8 +47,4 @@ def perfect(number: int) -> bool: if __name__ == "__main__": print("Program to check whether a number is a Perfect number or not.......") number = int(input("Enter number: ")) - check = perfect(number) - if check: - print("{} is a Perfect Number.".format(number)) - else: - print("{} is not a Perfect Number.".format(number)) + print(f"{number} is {'' if perfect(number) else 'not'} a Perfect Number.") From b0e9317be25f1c8f74913c2064861182438e5264 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 13:14:46 +0530 Subject: [PATCH 04/16] Made suggested changes --- maths/perfect_number.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index f7c9c7b07a7e..4bdcd6855016 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -16,6 +16,15 @@ def perfect(number: int) -> bool: divisors = [] for i in range(1, ((number // 2) + 1)): + """ + >>> perfect(27) + False + >>> perfect(28) + True + >>> perfect(29) + False + """ + """ starting from 1 as division by 0 will raise error. @@ -26,15 +35,6 @@ def perfect(number: int) -> bool: except 6 itself """ - """ - >>> perfect(27) - False - >>> perfect(28) - True - >>> perfect(29) - False - """ - if (number % i) == 0: divisors.append(i) @@ -47,4 +47,4 @@ def perfect(number: int) -> bool: if __name__ == "__main__": print("Program to check whether a number is a Perfect number or not.......") number = int(input("Enter number: ")) - print(f"{number} is {'' if perfect(number) else 'not'} a Perfect Number.") + print(f"{number} is {'' if perfect(number) else 'not '} a Perfect Number.") From c02b24ef7c92e733b0bfc5897989cbef92d3615f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 28 Jul 2020 09:58:55 +0200 Subject: [PATCH 05/16] Use generator expression --- maths/perfect_number.py | 52 ++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index 4bdcd6855016..3de742399f62 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -1,11 +1,10 @@ """ == Perfect Number == -In number theory, a perfect number is a positive integer -that is equal to the sum of its positive divisors, excluding -the number itself. -For 6 ==> divisors[1, 2, 3, 6] -Excluding 6 sum(divisors) = 1 + 2 + 3 = 6 -So, 6 is a Perfect Number +In number theory, a perfect number is a positive integer that is equal to the sum of +its positive divisors, excluding the number itself. +For example: 6 ==> divisors[1, 2, 3, 6] + Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6 + So, 6 is a Perfect Number Other examples of Perfect Numbers: 28, 486, ... @@ -14,37 +13,22 @@ def perfect(number: int) -> bool: - divisors = [] - for i in range(1, ((number // 2) + 1)): - """ - >>> perfect(27) - False - >>> perfect(28) - True - >>> perfect(29) - False - """ + """ + >>> perfect(27) + False + >>> perfect(28) + True + >>> perfect(29) + False - """ - starting from 1 as division by 0 - will raise error. - A number at most can be divisible - by the half of the number except - the number itself - 6 at most can be divisible by 3 - except 6 itself - """ - - if (number % i) == 0: - divisors.append(i) - - if sum(divisors) == number: - return True - else: - return False + Start from 1 because dividing by 0 will raise ZeroDivisionError. + A number at most can be divisible by the half of the number except the number + itself. For example, 6 is at most can be divisible by 3 except by 6 itself. + """ + return sum(i for i in range(1, ((number // 2) + 1)) if number % i == 0) == number if __name__ == "__main__": print("Program to check whether a number is a Perfect number or not.......") - number = int(input("Enter number: ")) + number = int(input("Enter number: ").strip()) print(f"{number} is {'' if perfect(number) else 'not '} a Perfect Number.") From bdf71e6c398a39eb30e458ef2a9cea34a42dc56c Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 22:12:59 +0530 Subject: [PATCH 06/16] Added Python Program to Check Krishnamurthy Number or not --- maths/krishnamurthy_number.py | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 maths/krishnamurthy_number.py diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py new file mode 100644 index 000000000000..e889f8e497a7 --- /dev/null +++ b/maths/krishnamurthy_number.py @@ -0,0 +1,40 @@ +""" +=== Krishnamurthy Number === +It is also known as Peterson Number +A Krishnamurthy Number is a number whose sum of the +factorial of the digits equals to the original +number itself. + +For example: 145 = 1! + 4! + 5! + So, 145 is a Krishnamurthy Number +""" + + +def factorial(digit: int) -> int: + return 1 if(digit == 0 or digit == 1) else (digit * factorial(digit - 1)) + + +def krishnamurthy(number: int) -> bool: + """ + >>> krishnamurthy(145) + True + + >>> krishnamurthy(240) + False + + >>> krishnamurthy(1) + """ + + + factSum = 0 + duplicate = number + while(duplicate > 0): + factSum += factorial(duplicate % 10) + duplicate //= 10 + return factSum == number + + +if __name__ == "__main__": + print("Program to check whether a number is a Krisnamurthy Number or not") + number = int(input("Enter number: ").strip()) + print(f"{number} is {'' if krishnamurthy(number) else 'not '} a Krishnamurthy Number.") From 1b73ff978c1ea9e48abb79a70516ab83b954b76e Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 22:31:50 +0530 Subject: [PATCH 07/16] Added Python Program to Check Krishnamurthy Number --- maths/krishnamurthy_number.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index e889f8e497a7..c8f63944fe18 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -11,7 +11,7 @@ def factorial(digit: int) -> int: - return 1 if(digit == 0 or digit == 1) else (digit * factorial(digit - 1)) + return 1 if (digit == 0 or digit == 1) else (digit * factorial(digit - 1)) def krishnamurthy(number: int) -> bool: @@ -25,10 +25,9 @@ def krishnamurthy(number: int) -> bool: >>> krishnamurthy(1) """ - factSum = 0 duplicate = number - while(duplicate > 0): + while duplicate > 0: factSum += factorial(duplicate % 10) duplicate //= 10 return factSum == number @@ -37,4 +36,6 @@ def krishnamurthy(number: int) -> bool: if __name__ == "__main__": print("Program to check whether a number is a Krisnamurthy Number or not") number = int(input("Enter number: ").strip()) - print(f"{number} is {'' if krishnamurthy(number) else 'not '} a Krishnamurthy Number.") + print( + f"{number} is {'' if krishnamurthy(number) else 'not '} a Krishnamurthy Number." + ) From 48f81711fbd460f6c728d7c865d2dcd69ef6a16b Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 23:09:10 +0530 Subject: [PATCH 08/16] Added Python Program to Check Krishnamurthy Number or not --- maths/krishnamurthy_number.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index c8f63944fe18..ec8990f958f5 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -1,5 +1,5 @@ """ -=== Krishnamurthy Number === + == Krishnamurthy Number == It is also known as Peterson Number A Krishnamurthy Number is a number whose sum of the factorial of the digits equals to the original @@ -11,11 +11,16 @@ def factorial(digit: int) -> int: + """ + function to calculate factorial of each digit + """ + return 1 if (digit == 0 or digit == 1) else (digit * factorial(digit - 1)) def krishnamurthy(number: int) -> bool: """ + >>> krishnamurthy(145) True @@ -23,6 +28,8 @@ def krishnamurthy(number: int) -> bool: False >>> krishnamurthy(1) + True + """ factSum = 0 From e6cdfd9d94cbb8515dc02efe263e547a584989dc Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 23:21:47 +0530 Subject: [PATCH 09/16] Build Error Fix - 1 --- maths/krishnamurthy_number.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index ec8990f958f5..1ca98e82f2c1 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -20,7 +20,6 @@ def factorial(digit: int) -> int: def krishnamurthy(number: int) -> bool: """ - >>> krishnamurthy(145) True @@ -29,9 +28,9 @@ def krishnamurthy(number: int) -> bool: >>> krishnamurthy(1) True - """ + factSum = 0 duplicate = number while duplicate > 0: From 9c8f15013d7ab3e352ecb9508b88c72ddaf25773 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 23:27:21 +0530 Subject: [PATCH 10/16] Build Error Fix - 2 --- maths/krishnamurthy_number.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index 1ca98e82f2c1..a4a7866dcfdb 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -30,7 +30,6 @@ def krishnamurthy(number: int) -> bool: True """ - factSum = 0 duplicate = number while duplicate > 0: From d86b4d24db90143a64728002f31fd6ef19e5658c Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 23:38:50 +0530 Subject: [PATCH 11/16] Fix Brackets positions --- maths/krishnamurthy_number.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index a4a7866dcfdb..752954ced804 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -41,6 +41,4 @@ def krishnamurthy(number: int) -> bool: if __name__ == "__main__": print("Program to check whether a number is a Krisnamurthy Number or not") number = int(input("Enter number: ").strip()) - print( - f"{number} is {'' if krishnamurthy(number) else 'not '} a Krishnamurthy Number." - ) + print(f"{number} is {'' if krishnamurthy(number) else 'not '} a Krishnamurthy Number.") From 348bd7fd5444261a4c6dfa05e2f9dd0f6ef055d4 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Tue, 28 Jul 2020 23:44:30 +0530 Subject: [PATCH 12/16] Fix Character Exceeding Error --- maths/krishnamurthy_number.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index 752954ced804..a4a7866dcfdb 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -41,4 +41,6 @@ def krishnamurthy(number: int) -> bool: if __name__ == "__main__": print("Program to check whether a number is a Krisnamurthy Number or not") number = int(input("Enter number: ").strip()) - print(f"{number} is {'' if krishnamurthy(number) else 'not '} a Krishnamurthy Number.") + print( + f"{number} is {'' if krishnamurthy(number) else 'not '} a Krishnamurthy Number." + ) From b47618e78854dff8e27df58daa89f9cc9b1c4df2 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Wed, 29 Jul 2020 13:19:23 +0530 Subject: [PATCH 13/16] Made Review Changes --- maths/krishnamurthy_number.py | 19 ++++++++++++++----- maths/perfect_number.py | 6 +++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index a4a7866dcfdb..48ca80c48725 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -12,10 +12,19 @@ def factorial(digit: int) -> int: """ + >>> factorial(3) + 6 + + >>> factorial(0) + 1 + + >>> factorial(5) + 120 + function to calculate factorial of each digit """ - return 1 if (digit == 0 or digit == 1) else (digit * factorial(digit - 1)) + return 1 if digit in (0, 1) else (digit * factorial(digit - 1)) def krishnamurthy(number: int) -> bool: @@ -33,14 +42,14 @@ def krishnamurthy(number: int) -> bool: factSum = 0 duplicate = number while duplicate > 0: - factSum += factorial(duplicate % 10) - duplicate //= 10 + duplicate, digit = divmod(duplicate, 10) + factSum += factorial(digit) return factSum == number if __name__ == "__main__": - print("Program to check whether a number is a Krisnamurthy Number or not") + print("Program to check whether a number is a Krisnamurthy Number or not.") number = int(input("Enter number: ").strip()) print( - f"{number} is {'' if krishnamurthy(number) else 'not '} a Krishnamurthy Number." + f"{number} is {'' if krishnamurthy(number) else 'not '}a Krishnamurthy Number." ) diff --git a/maths/perfect_number.py b/maths/perfect_number.py index 3de742399f62..148e988fb4c5 100644 --- a/maths/perfect_number.py +++ b/maths/perfect_number.py @@ -25,10 +25,10 @@ def perfect(number: int) -> bool: A number at most can be divisible by the half of the number except the number itself. For example, 6 is at most can be divisible by 3 except by 6 itself. """ - return sum(i for i in range(1, ((number // 2) + 1)) if number % i == 0) == number + return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number if __name__ == "__main__": - print("Program to check whether a number is a Perfect number or not.......") + print("Program to check whether a number is a Perfect number or not...") number = int(input("Enter number: ").strip()) - print(f"{number} is {'' if perfect(number) else 'not '} a Perfect Number.") + print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") From 939dcc53188a0856a0c54952633e67eabdfe1b59 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Wed, 29 Jul 2020 13:39:19 +0530 Subject: [PATCH 14/16] Build Error Fix - 3 --- maths/krishnamurthy_number.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index 48ca80c48725..3304f999c70e 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -19,8 +19,7 @@ def factorial(digit: int) -> int: 1 >>> factorial(5) - 120 - + 120 function to calculate factorial of each digit """ From b2a23f71dc3e79436c581540d3df74b710d34f58 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Wed, 29 Jul 2020 14:00:27 +0530 Subject: [PATCH 15/16] Build Error Fix - 4 --- maths/krishnamurthy_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index 3304f999c70e..503691d0b6fe 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -19,7 +19,7 @@ def factorial(digit: int) -> int: 1 >>> factorial(5) - 120 + 120 function to calculate factorial of each digit """ From 6f48f348a99e134c02d701a29baacab33d818426 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 29 Jul 2020 10:46:42 +0200 Subject: [PATCH 16/16] Update krishnamurthy_number.py --- maths/krishnamurthy_number.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py index 503691d0b6fe..c88f68a07f27 100644 --- a/maths/krishnamurthy_number.py +++ b/maths/krishnamurthy_number.py @@ -14,13 +14,10 @@ def factorial(digit: int) -> int: """ >>> factorial(3) 6 - >>> factorial(0) 1 - >>> factorial(5) 120 - function to calculate factorial of each digit """ return 1 if digit in (0, 1) else (digit * factorial(digit - 1)) @@ -30,10 +27,8 @@ def krishnamurthy(number: int) -> bool: """ >>> krishnamurthy(145) True - >>> krishnamurthy(240) False - >>> krishnamurthy(1) True """