Skip to content

changed folder name from number to math and added algorithm to find the kth divisor of a number #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions allalgorithms/math/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .max_numbers import find_max
from .kth_smallest_divisor import kth_divisor
29 changes: 29 additions & 0 deletions allalgorithms/math/kth_smallest_divisor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: UTF-8 -*-
#
# Find the k-th smallest divisor of a natural number.
# The All ▲lgorithms library for python
#
# Contributed by: Higor Santos
# Github: @higorsnt
#


def kth_divisor(number, k):
vector1 = []
vector2 = []

for i in range(1, int(number ** 0.5) + 1):
if number % i == 0:
vector1.append(i)

if i != int(number ** 0.5) + 1:
vector2.append(number // i)
vector2.reverse()

if k > (len(vector1) + len(vector2)):
raise ValueError("Doesn't Exist")
else:
if (k - 1) < len(vector1):
return vector1[k - 1]
else:
return vector2[k - len(vector1) - 1]
File renamed without changes.
1 change: 0 additions & 1 deletion allalgorithms/numeric/__init__.py

This file was deleted.

37 changes: 37 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest

from allalgorithms.math import (
find_max,
kth_divisor
)


class TestMath(unittest.TestCase):

def test_find_max_value(self):
test_list = [3, 1, 8, 7, 4]
self.assertEqual(8, find_max(test_list))

def test_find_kth_divisor_of_prime_number(self):
self.assertEqual(1, kth_divisor(257, 1))
self.assertEqual(257, kth_divisor(257, 2))
with self.assertRaises(ValueError):
kth_divisor(257, 3)

def test_find_kth_divisor_of_even_number(self):
self.assertEqual(7, kth_divisor(728, 4))
self.assertEqual(91, kth_divisor(728, 12))
self.assertEqual(364, kth_divisor(728, 15))
with self.assertRaises(ValueError):
kth_divisor(728, 17)

def test_find_kth_divisor_of_odd_number(self):
self.assertEqual(459, kth_divisor(162027, 9))
self.assertEqual(18003, kth_divisor(162027, 14))
self.assertEqual(162027, kth_divisor(162027, 16))
with self.assertRaises(ValueError):
kth_divisor(162027, 17)


if __name__ == "__main__":
unittest.main()
13 changes: 0 additions & 13 deletions tests/test_numeric.py

This file was deleted.