We need to write a function that finds deficient numbers from a list of integers. The problem statement: "You are given a list of integers. Write a function to find the 'deficient numbers'. A deficient number is a number for which the sum of its proper divisors is less than the number itself. Please write a function that solves this problem, and explain your algorithm."
So we need to produce a function that takes a list of integers and returns a list (or perhaps set) of deficient numbers. Also explain algorithm. Since it's typical coding problem, we need to implement proper divisor sum efficiently. But note: The list may contain positive integers? Proper divisors are defined for positive integers, usually for natural numbers > 0. For negative numbers, proper divisors? Not defined. So we can assume positive integers. Possibly 1? 1 has no proper divisors? Usually proper divisors exclude the number itself, so for 1, sum of proper divisors = 0, which is less than 1, so 1 is deficient. For perfect numbers sum equals number, abundant sum > number. So deficient: sum(proper divisors) < n.
We need to compute for each number in the list. Could be done by iterating over numbers and for each, compute sum of divisors up to sqrt(n). Complexity O(sqrt(n)) per number. If the list is large and numbers are large, we might need to optimize by precomputing something? But typical solution: for each n, compute sum of proper divisors by iterating i from 1 to sqrt(n), add i and n//i if distinct, and subtract n itself because we want proper divisors only (exclude n). Alternatively, we can compute sum of all divisors (including n) and subtract n. But careful: 1 is a divisor for all. Also if n is a perfect square, sqrt added only once.
Algorithm:
- For each number n in input list:
- If n <= 0? We could ignore or handle, but likely n > 0.
- Initialize sum_div = 1? But careful: 1 is always a proper divisor unless n=1. For n=1, proper divisors are none, sum = 0.
- So we need to handle n=1 separately: sum=0.
- For n>1: start with sum_div = 1, then iterate i from 2 to sqrt(n) (inclusive). If n % i == 0, then add i. If i != n // i, add n // i.
- Then compare sum_div < n? If yes, mark as deficient.
- Collect those numbers.
Alternatively, compute sum from 1 to sqrt: but we need to include 1 if n>1.
Edge cases: n=2: proper divisors: 1, sum=1 < 2 => deficient. n=3: same. n=4: proper divisors: 1,2 sum=3 < 4 => deficient? Actually 3<4, so deficient. n=5:1 sum=1<5. n=6: divisors 1,2,3 sum=6 = 6 => perfect, not deficient. n=12: 1+2+3+4+6=16>12 => abundant.
So function: def find_deficient(numbers): returns list of deficient numbers (preserving order? Probably yes). Could also return list of booleans? The problem says "find the deficient numbers", so likely returns list of those numbers.
Complexities: For each n, O(sqrt(n)). Overall O(m * sqrt(max_n)). That's acceptable.
But we could also precompute sums for all numbers up to max using sieve-like method: For all i from 1 to max, add i to multiples of i (excluding the multiple itself?) Actually proper divisor sum: for each i from 1 to max, for each multiple j of i where j != i, add i to sum_div[j]. That would be O(max_n log max_n) if we do for all up to max. Then we can check quickly for each number. But if the list has many numbers and max_n is large but we only need for numbers in list, we might compute individually. However, if the list has many numbers, precomputation could be more efficient if the max_n is not huge. But we can just do straightforward.
We'll present the algorithm: using proper divisor sum via sqrt decomposition.
Also note: For n=1, sum_proper = 0, which is less than 1, so deficient.
We'll write Python function.
Let's also include test cases.
We need to "explain your algorithm". So we will provide explanation in text.
Now produce final answer: a function with documentation, explanation.
We'll do:
def deficient_numbers(nums):
"""
Return a list of deficient numbers from the input list.
A number is deficient if sum of its proper divisors is less than the number.
Proper divisors: positive divisors excluding the number itself.
"""
result = []
for n in nums:
if n <= 0:
# Non-positive numbers are not considered deficient (or we can skip)
continue
if n == 1:
result.append(n)
continue
# sum of proper divisors, start with 1
sum_div = 1
# iterate from 2 to sqrt(n)
i = 2
while i * i <= n:
if n % i == 0:
sum_div += i
if i != n // i:
sum_div += n // i
i += 1
if sum_div < n:
result.append(n)
return result
We could optimize by using math.isqrt for sqrt.
Alternatively, we can compute using a helper function sum_proper_divisors(n).
We'll also mention time complexity O(k * sqrt(max)) where k = len(nums).
Now produce final answer with explanation and code.
But the instruction: "You are given a list of integers. Write a function to find the 'deficient numbers'." So we need to output the deficient numbers. Possibly return as list.
Also note: The problem might want to include negative numbers? But proper divisors for negative numbers are not defined; we skip them. But we could treat absolute value? Usually number theory deals with positive integers. So we'll assume positive.
We'll produce final answer accordingly.
Now produce final.
Solution Overview
A deficient number is a positive integer n for which the sum of its proper divisors (all positive divisors except n itself) is strictly less than n.
Given a list of integers, we need to return a list containing all deficient numbers from the input (preserving the original order). The algorithm computes the sum of proper divisors for each number efficiently using a square‑root decomposition.
Algorithm Explanation
For each positive integer n in the input:
-
Handle special cases
- If
n <= 0 → ignore (deficiency is defined only for positive integers).
- If
n == 1 → its proper divisor set is empty, sum = 0, so 1 is deficient.
-
Compute sum of proper divisors for n > 1
- Start with
sum_div = 1 (since 1 is a proper divisor of any n > 1).
- Loop
i from 2 up to ⌊√n⌋.
- If
i divides n (n % i == 0), then i is a divisor.
- Add
i to the sum.
- The complementary divisor is
n // i. If it differs from i (i.e., i is not the square root of n), add it as well.
- After the loop,
sum_div holds the sum of all proper divisors of n.
-
Check deficiency
- If
sum_div < n, the number n is deficient and is appended to the result list.
The loop runs at most √n iterations, so the time complexity per number is O(√n). For an input list of length m with maximum value M, the total complexity is O(m * √M). This is efficient for typical problem sizes.
Correctness Proof
We prove that the algorithm correctly identifies deficient numbers.
Lemma 1
For any n > 1, the procedure described in step 2 computes exactly the sum of all proper divisors of n.
Proof.
- The proper divisors of
n are all positive integers d such that 1 ≤ d < n and d | n.
- The algorithm iterates over every integer
i from 2 to ⌊√n⌋. For each i that divides n, both i and n/i are divisors.
- If
i = n/i (i.e., i is the square root of n), the divisor i appears only once, and the algorithm adds it once.
- The divisor
1 is added initially.
- The number
n itself is never added because the loop only considers divisors < n (since i ≤ √n < n and n/i ≥ √n but n/i equals n only when i = 1, which is handled separately).
- Thus every proper divisor appears exactly once in the sum, and no other numbers are added. ∎
Lemma 2
For n = 1, the algorithm treats it as deficient because the sum of proper divisors is 0 < 1.
Proof. By definition, 1 has no proper positive divisors. The algorithm explicitly handles this case and appends 1. ∎
Theorem
For any positive integer n in the input list, the algorithm includes n in the output iff n is deficient.
Proof.
- If part: Suppose the algorithm includes
n. That means after computing the sum S of proper divisors (by Lemma 1 or Lemma 2), it verified S < n. By definition, n is deficient.
- Only‑if part: Suppose
n is deficient. Then the sum of its proper divisors S satisfies S < n. The algorithm computes exactly that sum (again by Lemma 1/2) and will therefore satisfy the condition and include n. ∎
Numbers that are non‑positive are ignored, which matches the usual definition restricted to positive integers.
Reference Implementation (Python 3)
import math
from typing import List
def deficient_numbers(nums: List[int]) -> List[int]:
"""
Return a list of all deficient numbers from the input list `nums`.
A deficient number n satisfies: sum(proper divisors of n) < n.
Proper divisors exclude n itself.
Parameters
----------
nums : List[int]
List of integers (positive, zero, or negative). Only positive integers are considered.
Returns
-------
List[int]
Deficient numbers in the order they appear in `nums`.
"""
deficient = []
for n in nums:
if n <= 0:
# Skip non-positive numbers (deficiency defined for positive integers only)
continue
if n == 1:
deficient.append(1)
continue
# Sum of proper divisors, start with 1 (always a proper divisor for n>1)
sum_div = 1
# Iterate up to sqrt(n)
limit = math.isqrt(n)
for i in range(2, limit + 1):
if n % i == 0:
sum_div += i
other = n // i
if other != i: # avoid double-counting a square root
sum_div += other
if sum_div < n:
deficient.append(n)
return deficient
# Example usage:
if __name__ == "__main__":
test = [1, 2, 3, 4, 5, 6, 12, 18, 20, 24, 28, 30]
print(deficient_numbers(test)) # Output: [1, 2, 3, 4, 5] (6 is perfect, others abundant)
Explanation of code choices
math.isqrt gives the integer square root without floating‑point inaccuracies.
- The loop runs from
2 to limit, adding both divisors when they are distinct.
- We start
sum_div = 1 because 1 is a proper divisor for any n > 1.
- The check
sum_div < n exactly implements the deficient condition.
The function works in O(k * √M) time, where k = len(nums) and M = max(nums). It uses O(1) additional space besides the output list.