Posted on by Kalkicode
Code Number

# Deficient Number

The problem being addressed is to determine whether a given number is a Deficient number or not. A Deficient number is a positive integer for which the sum of its proper divisors (excluding itself) is less than the number itself. This concept is used to classify numbers based on their properties and relationships with their divisors.

## Problem Statement and Description

Given a positive integer, the goal is to determine whether it is a Deficient number or not. A Deficient number is one where the sum of its proper divisors (excluding itself) is less than the number itself. For example, the number 15 has divisors 1, 3, and 5. The sum of these divisors is 9, which is less than 15, making 15 a Deficient number.

The first few deficient numbers are: 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 29, 31, 32, 33, 34, 35, 37, 38, 39, 41, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 55, 57, 58, 59, 61, 62, 63, 65, 67, 68, 69, 71, 73, 74, 75, 76, 77, 79, 81, 82, 83, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 97, 98, 99, 100, 101, etc.

## Idea to Solve the Problem

To determine if a number is a Deficient number, we need to calculate the sum of its proper divisors (excluding itself) and compare it with the number itself multiplied by 2. If the sum is less than 2 times the number, then it is a Deficient number.

## Pseudocode

``````divisor_sum(number):
result = number
for i from 1 to number/2:
if number % i == 0:
result += i
return result

is_deficient(number):
if divisor_sum(number) < 2 * number:
print number, "Is a Deficient Number"
else:
print number, "Is not a Deficient Number"
``````

## Algorithm Explanation

1. Create a function `divisor_sum(number)` that calculates the sum of the proper divisors (excluding itself) of the given number. Iterate through numbers from 1 to `number/2`. If the current number divides `number` evenly (i.e., `number % i == 0`), add it to the result.
2. Create a function `is_deficient(number)` that determines whether a number is a Deficient number or not. Call the `divisor_sum` function to get the sum of proper divisors. Compare the sum with 2 times the number to determine if it is a Deficient number.
3. In the `main()` function, call `is_deficient` for different test cases.

## Code Solution

``````//C Program
//Check if a given number is deficient or not
#include <stdio.h>

// Function which is calculate sum of divisors in given number
int divisor_sum(int number)
{
//numbers are always on itself divide
int result = number;

for (int i=1; i<= number/2; i++)
{
if (number%i==0)
{
//When number is divide by i
result += i;
}
}

return result;
}
//Function are show the result number is deficient or not
void is_deficient(int number)
{
//Check whether number divisor sum is less than twice given number?
if(divisor_sum(number) < 2 * number)
{
//When Yes
printf("%d Is an Deficient Number\n",number);
}
else
{
//When No
printf("%d Is not a Deficient Number\n",number);
}
}

int main() {
//Test Cases
is_deficient(15);
is_deficient(20);
is_deficient(12);
is_deficient(17);
is_deficient(23);
return 0;
}
```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number``````
``````/*
C++ Program
Check if a given number is deficient or not
*/
#include<iostream>
using namespace std;

class MyNumber {
public:

// Function which is calculate sum of divisors in given number
int divisor_sum(int number) {
//numbers are always on itself divide
int result = number;
for (int i = 1; i <= number / 2; i++) {
if (number % i == 0) {
//When number is divide by i
result += i;
}
}
return result;
}
//Function are show the result number is deficient or not
void is_deficient(int number) {
//Check whether number divisor sum is less than twice given number?

if (this->divisor_sum(number) < 2 *number) {
//When Yes

cout << number << " Is an Deficient Number\n";
} else {
//When No

cout << number << " Is not a Deficient Number\n";
}
}
};
int main() {
MyNumber obj ;
// Test Case
obj.is_deficient(15);
obj.is_deficient(20);
obj.is_deficient(12);
obj.is_deficient(17);
obj.is_deficient(23);
return 0;
}```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number``````
``````/*
Java Program
Check if a given number is deficient or not
*/

public class MyNumber {

// Function which is calculate sum of divisors in given number
public int divisor_sum(int number)
{
//numbers are always on itself divide
int result = number;

for (int i=1; i<= number/2; i++)
{
if (number%i==0)
{
//When number is divide by i
result += i;
}
}

return result;
}
//Function are show the result number is deficient or not
public void is_deficient(int number)
{
//Check whether number divisor sum is less than twice given number?
if(divisor_sum(number) < 2 * number)
{
//When Yes
System.out.print(number+" Is an Deficient Number\n");
}
else
{
//When No
System.out.print(number+" Is not a Deficient Number\n");
}
}
public static void main(String[] args) {

MyNumber obj = new MyNumber();
// Test Case
obj.is_deficient(15);
obj.is_deficient(20);
obj.is_deficient(12);
obj.is_deficient(17);
obj.is_deficient(23);

}
}```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number``````
``````/*
C# Program
Check if a given number is deficient or not
*/
using System;
public class MyNumber {

// Function which is calculate sum of divisors in given number
public int divisor_sum(int number) {
//numbers are always on itself divide
int result = number;

for (int i = 1; i <= number / 2; i++) {
if (number % i == 0) {
//When number is divide by i
result += i;
}
}

return result;
}
//Function are show the result number is deficient or not
public void is_deficient(int number) {
//Check whether number divisor sum is less than twice given number?
if (divisor_sum(number) < 2 * number) {
//When Yes
Console.Write(number + " Is an Deficient Number\n");
} else {
//When No
Console.Write(number + " Is not a Deficient Number\n");
}
}
public static void Main(String[] args) {

MyNumber obj = new MyNumber();
// Test Case
obj.is_deficient(15);
obj.is_deficient(20);
obj.is_deficient(12);
obj.is_deficient(17);
obj.is_deficient(23);

}
}```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number``````
``````# Python 3 Program
# Check if a given number is deficient or not

class MyNumber :
# Function which is calculate sum of divisors in given number
def divisor_sum(self, number) :
#numbers are always on itself divide
result = number
i = 1
while (i <= int(number / 2)) :
if (number % i == 0) :
#When number is divide by i
result += i

i += 1

return result

#Function are show the result number is deficient or not
def is_deficient(self, number) :
#Check whether number divisor sum is less than twice given number?

if (self.divisor_sum(number) < 2 * number) :
print(number ," Is an Deficient Number")
else :
print(number ," Is not a Deficient Number")

def main() :
obj = MyNumber()
#Test Case
obj.is_deficient(15)
obj.is_deficient(20)
obj.is_deficient(12)
obj.is_deficient(17)
obj.is_deficient(23)

if __name__ == "__main__":
main()```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number``````
``````# Ruby Program
# Check if a given number is deficient or not

class MyNumber
# Function which is calculate sum of divisors in given number
def divisor_sum(number)
#numbers are always on itself divide
result = number
i = 1
while (i <= number / 2)
if (number % i == 0)
#When number is divide by i
result += i
end
i += 1
end
return result
end
#Function are show the result number is deficient or not
def is_deficient(number)
#Check whether number divisor sum is less than twice given number?

if (self.divisor_sum(number) < 2 * number)
print(number ," Is an Deficient Number\n")
else
print(number ," Is not a Deficient Number\n")
end
end
end
def main()
obj = MyNumber.new()
#Test Case
obj.is_deficient(15)
obj.is_deficient(20)
obj.is_deficient(12)
obj.is_deficient(17)
obj.is_deficient(23)
end
main()```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number
``````
``````/*
Scala Program
Check if a given number is deficient or not
*/
class MyNumber {
// Function which is calculate sum of divisors in given number
def divisor_sum(number: Int): Int = {
//numbers are always on itself divide
var result: Int = number;
var i: Int = 1;
while (i <= number / 2) {
if (number % i == 0) {
//When number is divide by i
result += i;
}
i += 1;
}
return result;
}
//Function are show the result number is deficient or not
def is_deficient(number: Int): Unit = {
//Check whether number divisor sum is less than twice given number?

if (this.divisor_sum(number) < 2 * number) {
print(s"\$number Is an Deficient Number\n");
} else {
print(s"\$number Is not a Deficient Number\n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Case
obj.is_deficient(15);
obj.is_deficient(20);
obj.is_deficient(12);
obj.is_deficient(17);
obj.is_deficient(23);
}
}```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number``````
``````/*
Swift 4 Program
Check if a given number is deficient or not
*/
class MyNumber {
// Function which is calculate sum of divisors in given number
func divisor_sum(_ number: Int) -> Int {
//numbers are always on itself divide
var result: Int = number;
var i: Int = 1;
while (i <= number / 2) {
if (number % i == 0) {
//When number is divide by i
result += i;
}
i += 1;
}
return result;
}
//Function are show the result number is deficient or not
func is_deficient(_ number: Int) {
//Check whether number divisor sum is less than twice given number?

if (self.divisor_sum(number) < 2 * number) {
print(number ," Is an Deficient Number");
} else {
print(number ," Is not a Deficient Number");
}
}
}
func main() {
let obj: MyNumber = MyNumber();
//Test Case
obj.is_deficient(15);
obj.is_deficient(20);
obj.is_deficient(12);
obj.is_deficient(17);
obj.is_deficient(23);
}
main();```
```

#### Output

``````15  Is an Deficient Number
20  Is not a Deficient Number
12  Is not a Deficient Number
17  Is an Deficient Number
23  Is an Deficient Number``````
``````<?php
/*
Php Program
Check if a given number is deficient or not
*/
class MyNumber {
// Function which is calculate sum of divisors in given number

public 	function divisor_sum(\$number) {
//numbers are always on itself divide
\$result = \$number;
for (\$i = 1; \$i <= intval(\$number / 2); \$i++) {
if (\$number % \$i == 0) {
//When number is divide by i
\$result += \$i;
}
}
return \$result;
}
//Function are show the result number is deficient or not

public 	function is_deficient(\$number) {
//Check whether number divisor sum is less than twice given number?

if (\$this->divisor_sum(\$number) < 2 *\$number) {
//When Yes

echo(\$number ." Is an Deficient Number\n");
} else {
//When No

echo(\$number ." Is not a Deficient Number\n");
}
}
};

function main() {
\$obj = new MyNumber();
// Test Case

\$obj->is_deficient(15);
\$obj->is_deficient(20);
\$obj->is_deficient(12);
\$obj->is_deficient(17);
\$obj->is_deficient(23);
}
main();```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number``````
``````/*
Node Js Program
Check if a given number is deficient or not
*/
class MyNumber {
// Function which is calculate sum of divisors in given number
divisor_sum(number) {
//numbers are always on itself divide
var result = number;
for (var i = 1; i <= parseInt(number / 2); i++) {
if (number % i == 0) {
//When number is divide by i
result += i;
}
}
return result;
}
//Function are show the result number is deficient or not
is_deficient(number) {
//Check whether number divisor sum is less than twice given number?

if (this.divisor_sum(number) < 2 *number) {
//When Yes

process.stdout.write(number + " Is an Deficient Number\n");
} else {
//When No

process.stdout.write(number + " Is not a Deficient Number\n");
}
}
}

function main(args) {
var obj = new MyNumber();
// Test Case
obj.is_deficient(15);
obj.is_deficient(20);
obj.is_deficient(12);
obj.is_deficient(17);
obj.is_deficient(23)
}
main();```
```

#### Output

``````15 Is an Deficient Number
20 Is not a Deficient Number
12 Is not a Deficient Number
17 Is an Deficient Number
23 Is an Deficient Number``````

## Time Complexity Analysis

For a given number `n`, the `divisor_sum` function iterates through numbers from 1 to `n/2`, checking for divisibility. Therefore, the time complexity of the `divisor_sum` function is O(n/2), which simplifies to O(n). The `is_deficient` function calls `divisor_sum` and performs a constant-time comparison. Overall, the algorithm's time complexity is O(n), where n is the given number.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post