Repository: keon/algorithms
Pull request #728 (2020-10-18 18:23:20)

Add Krishnamurty Number to Math algorithms 
I have added the Krishnamurthy Number checker algorithm. This is a common algorithm that is pretty famous here in India especially from a beginner's perspective. 
Talking of Krishnamurthy number,  it's a number whose sum total of the factorials of each digit is equal to the number itself.

**hacktoberfest-accepted**

ericklarac: Could you add some tests under the test folder?
Nanak360: Yes, I'll surely do that @ericklarac.
I would also like to add that I am new to opensource, motivated by Hacktoberfest. Would you please help me to make this PR count as one of my Hacktoberfest PRs? 
If you'll just add a "hacktoberfest-accepted" label to this Issue, it'll mean a lot to me.
Nanak360: For some reason, the tests failed.
I'm not understanding why. Would someone help me out?

ericklarac: > For some reason, the tests failed.
> I'm not understanding why. Would someone help me out?

Traceback (most recent call last):
  File "/home/travis/build/keon/algorithms/tests/test_maths.py", line 329, in test_factorial
    self.assertEqual(637816310, factorial(34521, 10**9 + 7))
TypeError: factorial() takes 1 positional argument but 2 were given.

The factorial function was previously defined in other file. You are exporting your function, and it is overriding the other function.

A workaround could be to rename the `factorial` function on `krishnamurthy_number.py`, or try only to export `krishnamurthy_number` on the `algorithms/maths/__init__.py` file.
Nanak360: Okay, I am doing the changes.
Could you please a "hacktoberfest-accepted" label to this PR?

Patch:
diff --git a/README.md b/README.md
index a20a740..3ba94d3 100644
--- a/README.md
+++ b/README.md
@@ -209,6 +209,7 @@ If you want to uninstall algorithms, it is as simple as:
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
+    - [krishnamurthy_number](algorithms/maths/krishnamurthy_number.py)
     - [modular_exponential](algorithms/maths/modular_exponential.py)
     - [modular_inverse](algorithms/maths/modular_inverse.py)
     - [next_bigger](algorithms/maths/next_bigger.py)
diff --git a/algorithms/maths/__init__.py b/algorithms/maths/__init__.py
index 678d923..b29b33b 100644
--- a/algorithms/maths/__init__.py
+++ b/algorithms/maths/__init__.py
@@ -19,3 +19,4 @@ from .find_order_simple import *
 from .find_primitive_root_simple import *
 from .diffie_hellman_key_exchange import *
 from .power import *
+from .krishnamurthy_number import *
\ No newline at end of file
diff --git a/algorithms/maths/krishnamurthy_number.py b/algorithms/maths/krishnamurthy_number.py
new file mode 100644
index 0000000..a0a451e
--- /dev/null
+++ b/algorithms/maths/krishnamurthy_number.py
@@ -0,0 +1,43 @@
+"""
+A Krishnamurthy number is a number whose sum total of the factorials of each digit is equal to the number itself.
+
+Here's what I mean by that:
+
+"145" is a Krishnamurthy Number because,
+1! + 4! + 5! = 1 + 24 + 120 = 145
+
+"40585" is also a Krishnamurthy Number.
+4! + 0! + 5! + 8! + 5! = 40585
+
+"357" or "25965" is NOT a Krishnamurthy Number
+3! + 5! + 7! = 6 + 120 + 5040 != 357
+
+The following function will check if a number is a Krishnamurthy Number or not and return a boolean value.
+"""
+
+
+def find_factorial(n):
+    fact = 1
+    while n != 0:
+        fact *= n
+        n -= 1
+    return fact
+
+
+def krishnamurthy_number(n):
+    if n == 0:
+        return False
+    sum_of_digits = 0   # will hold sum of FACTORIAL of digits
+    temp = n
+
+    while temp != 0:
+
+        # get the factorial of of the last digit of n and add it to sum_of_digits
+        sum_of_digits += find_factorial(temp % 10)
+
+        # replace value of temp by temp/10
+        # i.e. will remove the last digit from temp
+        temp //= 10
+
+    # returns True if number is krishnamurthy
+    return (sum_of_digits == n)


Test patch:
diff --git a/tests/test_maths.py b/tests/test_maths.py
index 7667488..f5586b6 100644
--- a/tests/test_maths.py
+++ b/tests/test_maths.py
@@ -21,7 +21,8 @@ from algorithms.maths import (
     cosine_similarity,
     find_order,
     find_primitive_root,
-    alice_private_key, alice_public_key, bob_private_key, bob_public_key, alice_shared_key, bob_shared_key, diffie_hellman_key_exchange
+    alice_private_key, alice_public_key, bob_private_key, bob_public_key, alice_shared_key, bob_shared_key, diffie_hellman_key_exchange,
+    krishnamurthy_number,
 )
 
 import unittest
@@ -393,6 +394,21 @@ class TestFindOrder(unittest.TestCase):
         self.assertEqual(-1, find_order(128, 256))
         self.assertEqual(352, find_order(3, 353))
 
+class TestKrishnamurthyNumber(unittest.TestCase):
+    """[summary]
+    Test for the file krishnamurthy_number.py
+
+    Arguments:
+        unittest {[type]} -- [description]
+    """
+    
+    def test_krishnamurthy_number(self):
+        self.assertFalse(krishnamurthy_number(0))
+        self.assertTrue(krishnamurthy_number(2))
+        self.assertTrue(krishnamurthy_number(1))
+        self.assertTrue(krishnamurthy_number(145))
+        self.assertTrue(krishnamurthy_number(40585))
+
 
 class TestDiffieHellmanKeyExchange(unittest.TestCase):
     """[summary]