Skip to content

Commit 30512d9

Browse files
Nanak360Nanak Bandyopadhyay
andauthored
Add Krishnamurty Number to Math algorithms (keon#728)
* add Krishnamurthy Number to Math algorithms * tests added for krishnamurthy number, readme update * Update function name of krishnamurthy_number.py Co-authored-by: Nanak Bandyopadhyay <nanakbandyopadhay@hotmail.com>
1 parent bc0b1d3 commit 30512d9

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ If you want to uninstall algorithms, it is as simple as:
209209
- [gcd/lcm](algorithms/maths/gcd.py)
210210
- [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
211211
- [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
212+
- [krishnamurthy_number](algorithms/maths/krishnamurthy_number.py)
212213
- [modular_exponential](algorithms/maths/modular_exponential.py)
213214
- [modular_inverse](algorithms/maths/modular_inverse.py)
214215
- [next_bigger](algorithms/maths/next_bigger.py)

algorithms/maths/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
from .find_primitive_root_simple import *
2020
from .diffie_hellman_key_exchange import *
2121
from .power import *
22+
from .krishnamurthy_number import *
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
A Krishnamurthy number is a number whose sum total of the factorials of each digit is equal to the number itself.
3+
4+
Here's what I mean by that:
5+
6+
"145" is a Krishnamurthy Number because,
7+
1! + 4! + 5! = 1 + 24 + 120 = 145
8+
9+
"40585" is also a Krishnamurthy Number.
10+
4! + 0! + 5! + 8! + 5! = 40585
11+
12+
"357" or "25965" is NOT a Krishnamurthy Number
13+
3! + 5! + 7! = 6 + 120 + 5040 != 357
14+
15+
The following function will check if a number is a Krishnamurthy Number or not and return a boolean value.
16+
"""
17+
18+
19+
def find_factorial(n):
20+
fact = 1
21+
while n != 0:
22+
fact *= n
23+
n -= 1
24+
return fact
25+
26+
27+
def krishnamurthy_number(n):
28+
if n == 0:
29+
return False
30+
sum_of_digits = 0 # will hold sum of FACTORIAL of digits
31+
temp = n
32+
33+
while temp != 0:
34+
35+
# get the factorial of of the last digit of n and add it to sum_of_digits
36+
sum_of_digits += find_factorial(temp % 10)
37+
38+
# replace value of temp by temp/10
39+
# i.e. will remove the last digit from temp
40+
temp //= 10
41+
42+
# returns True if number is krishnamurthy
43+
return (sum_of_digits == n)

tests/test_maths.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
cosine_similarity,
2222
find_order,
2323
find_primitive_root,
24-
alice_private_key, alice_public_key, bob_private_key, bob_public_key, alice_shared_key, bob_shared_key, diffie_hellman_key_exchange
24+
alice_private_key, alice_public_key, bob_private_key, bob_public_key, alice_shared_key, bob_shared_key, diffie_hellman_key_exchange,
25+
krishnamurthy_number,
2526
)
2627

2728
import unittest
@@ -393,6 +394,21 @@ def test_find_order_simple(self):
393394
self.assertEqual(-1, find_order(128, 256))
394395
self.assertEqual(352, find_order(3, 353))
395396

397+
class TestKrishnamurthyNumber(unittest.TestCase):
398+
"""[summary]
399+
Test for the file krishnamurthy_number.py
400+
401+
Arguments:
402+
unittest {[type]} -- [description]
403+
"""
404+
405+
def test_krishnamurthy_number(self):
406+
self.assertFalse(krishnamurthy_number(0))
407+
self.assertTrue(krishnamurthy_number(2))
408+
self.assertTrue(krishnamurthy_number(1))
409+
self.assertTrue(krishnamurthy_number(145))
410+
self.assertTrue(krishnamurthy_number(40585))
411+
396412

397413
class TestDiffieHellmanKeyExchange(unittest.TestCase):
398414
"""[summary]

0 commit comments

Comments
 (0)