Skip to content

Commit

Permalink
challenge_28_p
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Glucose committed Jan 10, 2025
1 parent e906db8 commit 08bf491
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
47 changes: 47 additions & 0 deletions solutions/challenge_28_p/perfect_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for identifying perfect numbers in a given range
Created on 06 Jan 2025
@author: Arthur (Mr-Glucose)
"""


def get_divisors(n):
"""Checks if a list of divisors of n, excluding n itself"""
if n <= 0:
raise ValueError("Input must be a positive integer greater than 0")

return [i for i in range(1, n) if n % i == 0]


def is_perfect_number(n):
"""
Checks if a given number is perfect.
"""
if n <= 0:
raise ValueError("Input must be a positive integer greater than 0")

return sum(get_divisors(n)) == n


def perfect_numbers_in_range(start, end):
"""
Returns all perfect numbers in a specified range.
"""
if start <= 0 or end <= 0:
raise ValueError("Both start and end must be positive integers")
if start > end:
raise ValueError("Start should not be greater than end")

return [num for num in range(start, end + 1) if is_perfect_number(num)]


if __name__ == "__main__":
try:
# Example usage
found_perfect_numbers = perfect_numbers_in_range(1, 1000)
print("Perfect numbers between 1 and 1000:", found_perfect_numbers)
except ValueError as e:
print(f"Error: {e}")
52 changes: 52 additions & 0 deletions solutions/tests/challenge_28_p/test_perfect_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for identifying perfect numbers in a given range
Created on 06 Jan 2025
@author: Arthur (Mr-Glucose)
"""

import unittest

from solutions.challenge_28_p.perfect_numbers import (
get_divisors,
is_perfect_number,
perfect_numbers_in_range,
)


class TestPerfectNumbers(unittest.TestCase):
"""
Test suite for the Perfect Numbers module.
This class tests the functionality of the functions:
get_divisors, is_perfect_number, and perfect_numbers_in_range.
"""

def test_get_divisors_valid_input(self):
"""
Tests the get_divisors function with valid input.
Checks that the divisors of a number are correctly returned.
"""
self.assertEqual(get_divisors(6), [1, 2, 3])
self.assertEqual(get_divisors(28), [1, 2, 4, 7, 14])

def test_is_perfect_number(self):
"""
Tests the is_perfect_number function.
Checks whether a number is a perfect number.
"""
self.assertTrue(is_perfect_number(6))
self.assertTrue(is_perfect_number(28))
self.assertFalse(is_perfect_number(12))

def test_perfect_numbers_in_range(self):
"""
Tests the perfect_numbers_in_range function.
Checks whether the function returns perfect numbers in a given range.
"""
self.assertEqual(perfect_numbers_in_range(1, 100), [6, 28])
self.assertEqual(perfect_numbers_in_range(1, 1000), [6, 28, 496])


if __name__ == "__main__":
unittest.main()

0 comments on commit 08bf491

Please sign in to comment.