Skip to content

Commit

Permalink
sum_proper_divisors solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahim-Elmisbah committed Jan 8, 2025
1 parent 5869fce commit afb3b0b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
46 changes: 46 additions & 0 deletions solutions/sum_proper_divisors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for calculating the sum of proper divisors of a number.
Module contents:
- sum_proper_divisors: calculates the sum of proper divisors of a given number.
Created on Jan 08 2025
@author: Ibrahim Elmisbah
"""


def sum_proper_divisors(n: int) -> int:
"""Calculate the sum of proper divisors of a number.
A proper divisor of a number is a divisor that is less than the number itself.
Parameters:
n: int, the input number (must be positive)
Returns -> int: the sum of proper divisors of the number
Raises:
AssertionError: if the argument is not a positive integer
>>> sum_proper_divisors(6)
6
>>> sum_proper_divisors(10)
8
>>> sum_proper_divisors(1)
0
"""
assert isinstance(n, int), "Input must be an integer"
assert n > 0, "Input must be a positive integer"

# Initialize the sum of divisors
total = 0

# Check all possible divisors from 1 to n // 2
for i in range(1, n // 2 + 1):
# If 'i' divides 'n' without a remainder, it is a divisor
if n % i == 0:
total += i # Add 'i' to the total sum of proper divisors

return total
46 changes: 46 additions & 0 deletions solutions/tests/test_sum_proper_divisors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest

from ..sum_proper_divisors import sum_proper_divisors


class TestSumProperDivisors(unittest.TestCase):
"""Tests for the sum_proper_divisors function."""

def test_small_number(self):
"""It should return the correct sum for a small number."""
actual = sum_proper_divisors(6)
expected = 6
self.assertEqual(actual, expected)

def test_prime_number(self):
"""It should return 1 for a prime number."""
actual = sum_proper_divisors(7)
expected = 1
self.assertEqual(actual, expected)

def test_one(self):
"""It should return 0 for the number 1."""
actual = sum_proper_divisors(1)
expected = 0
self.assertEqual(actual, expected)

def test_large_number(self):
"""It should return the correct sum for a large number."""
actual = sum_proper_divisors(28)
expected = 28
self.assertEqual(actual, expected)

def test_non_integer(self):
"""It should raise AssertionError for non-integer input."""
with self.assertRaises(AssertionError):
sum_proper_divisors(10.5)

def test_negative_number(self):
"""It should raise AssertionError for a negative number."""
with self.assertRaises(AssertionError):
sum_proper_divisors(-5)

def test_zero(self):
"""It should raise AssertionError for zero input."""
with self.assertRaises(AssertionError):
sum_proper_divisors(0)

0 comments on commit afb3b0b

Please sign in to comment.