Deficient Numbers in C++20 May 2025 | 4 min read A deficient number is a positive integer where the sum of its proper divisors (excluding the number itself) is less than the number. For example, 8 is deficient because its divisors (1, 2, 4) sum to 7, which is less than 8. Input: 10 Output: Deficient Input: 12 Output: Not Deficient Explanation: For the number 10, its proper divisors are 1, 2, and 5. Adding them gives a sum of 8, which is less than 10, making 10 a deficient number. For 12, its proper divisors are 1, 2, 3, 4, and 6, with a sum of 16, which is greater than 12, so 12 is not deficient. Approach 1: Using Divisor Sum Comparison:Algorithm: If a number is deficientStep 1: Understand the Problem: Identify the goal: check whether a given number n is deficient. To do this, calculate the sum of its proper divisors and compare it with n. A number is deficient if the sum is less than n. Step 2: Define Deficient Number: A deficient number is a positive integer where the sum of its proper divisors (excluding the number itself) is less than the number. The proper divisors are all integers less than the number that divide it evenly, without including the number itself. For example, 8 is deficient because the sum of its divisors (1, 2, 4) is 7, which is less than 8. Step 3: Input the Number: Start with an input number n. This number should be a positive integer, as deficiency is only defined for positive integers. Step 4: Initialize Variables: Set up a variable sum to store the cumulative total of the divisors. Initialize sum=0 before starting the divisor calculation. Step 5: Find Proper Divisors: Proper divisors of a number are all integers less than n that divide n evenly (remainder is 0). Loop through all integers from 1 to n-1. For each number i, check if n mod i=0 (i.e., i divides n without remainder). If true, add i to sum. Step 6: Calculate Sum of Divisors: At the end of the loop, sum will hold the total value of all proper divisors of n. Step 7: Compare the Sum with n: If sum<n, then n is a deficient number because the sum of its proper divisors is smaller than n. Otherwise, n is not a deficient number. Step 8: Output the Result: Print the result based on the comparison:"Deficient" if sum<n. Otherwise, "Not Deficient". Algorithm: If a number is not deficientStep 1: Understand the Problem: A number is not deficient if the sum of its proper divisors (excluding the number itself) is greater than or equal to the number. The task is to check if this condition holds for a given number n. Step 2: Input the Number: Take a positive integer n as the input. Only positive integers are valid for this determination. Step 3: Initialize Variables: Set up a variable sum to accumulate the total of proper divisors. Initialize it to zero: sum=0 Step 4: Identify Proper Divisors: Proper divisors are all integers smaller than n that divide n without a remainder. Use a loop to iterate through numbers from 1 to n-1. For each number i, check if n modi=0. If the condition is true, i is a proper divisor. Add i to sum. Step 5: Calculate the Sum of Divisors: As the loop completes, sum will hold the sum of all proper divisors of n. Step 6: Compare sum with n: If sum≥n, the number n is not deficient because its divisors are sufficient or exceed n. If sum<n, the number is deficient. Step 7: Output the Result: Display the result: "Not Deficient" if sum≥n. "Deficient" otherwise. Program:Output: 10 is Deficient 12 is Not Deficient Complexity Analysis:Time Complexity:The time complexity of determining whether a number is deficient is O(n), where n is the input number. This is because we need to check each integer from 1 to n-1 to find the divisors. Thus, the process involves iterating up to n. Space Complexity:The space complexity of determining whether a number is deficient is O(1), since the algorithm only requires a fixed amount of extra space. It uses a single variable to sum divisors and does not store any large data structures, regardless of the input size. Approach: Efficient Divisor Calculation (Using √n)The above approach loops through all numbers from 111 to n-1n-1n-1 to find proper divisors, which has a time complexity of O(n)O(n)O(n). A more efficient method can reduce this complexity by considering the divisors in pairs and iterating only up to n\sqrt{n}n. |
Difference between STL Algorithms and Custom Algorithms
Introduction The algorithm in C++ programming is used in an algorithmic form to perform operations on data structures that are efficient. Algorithms are broadly classified into two categories: STL algorithms and Custom algorithms. They work in different ways and benefit the projects in diversified ways according to...
10 min read
std::exponential_distribution in C++
Introduction The main topic of this article is the std::exponential_distribution class in C++, which is quite a useful tool from the Standard Library to form random numbers with exponential distribution. This distribution finds an application whenever the time between events in a Poisson process is of interest,...
6 min read
std::common_type T1, T2 ::type function in C++
std::common_type<T1, T2>::type function in C++ In this article, we will discuss the std::common_type<T1, T2>::type function in C++ with its syntax, parameters, key concepts, and example. What is the std::common_type<T1, T2>::type function in C++? In C++, the common type among a list of types is identified via the std::common_type...
4 min read
Gomory-Hu Tree Construction in C++
A Gomory-Hu tree is a compressed representation of the minimum cut values between every pair of nodes in an undirected graph. The tree can be used to solve network flow, min-cut, and connectivity-type problems very efficiently. In the Gomory-Hu tree, each edge indicates a minimum cut...
8 min read
Distinct Subsequences in C++
In the realm of computer science, particularly in string processing and combinatorics, the concept of distinct subsequences holds a significant place. A subsequence is derived from a string by deleting some or no characters without changing the order of the remaining characters. The problem of finding...
15 min read
Difference between #include iostream and #include stdio.h
Difference between #include<iostream> and #include<stdio.h> In this article, we will discuss the differences between #include<iostream> and #include<stdio.h>. Before going to the differences let's understand each term. What is the #include<iostream>? The term iostream means standard input-output stream. The header file iostream declares objects that control the reading operations...
5 min read
Program to print V and inverted-V pattern in C++
In this article, we will discuss the Program to print V and inverted-V patterns in C++ with its implementations, time complexity, and space complexity. 1. Inverted V pattern: Print the pattern inverted V given the value n. Example 1: Let us take an example to illustrate the inverted-V...
8 min read
Nim 21 Game in C++
The Nim 21 Game is a variant of Nim, the classic mathematical game of Nim that is used to exemplify the combinatorial game of theoretic principles. The last object is taken by the winners in Nim games; other variations have players take an object from a...
16 min read
C++ program to construct graph with certain conditions
In this article, we will discuss a C++ program to construct graphs with certain conditions. Basic data structures called graphs, which are used to display the relationships between entities. In many applications, it is crucial to build graphs that adhere to particular constraints or requirements. These specifications...
6 min read
Simulated Annealing Algorithm in C++
Optimization problems are ubiquitous in the fields of science, engineering, and technology. From designing efficient circuits to planning delivery routes, optimizing solutions is a fundamental task that requires powerful algorithms. However, many real-world optimization problems are non-linear, complex, and riddled with local optima, making it...
13 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India