Abundant Number
The problem being addressed is to determine whether a given number is an abundant number or not. An abundant number is a positive integer that is smaller than the sum of its proper divisors (excluding 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 an abundant number or not. An abundant number is one where the sum of its proper divisors is greater than the number itself. For instance, the number 20 has divisors 1, 2, 4, 5, and 10. The sum of these divisors is 22, which is greater than 20, making 20 an abundant number.
Idea to Solve the Problem
To determine if a number is abundant or not, we can calculate the sum of its proper divisors and compare it with the number itself. If the sum is greater, the number is abundant; otherwise, it is not.
Pseudocode
divisor_sum(number):
result = 0
for i from 1 to number/2:
if number % i == 0:
result += i
return result
is_abundant(number):
if divisor_sum(number) > number:
print number, "Is an Abundant Number"
else:
print number, "Is not an Abundant Number"
main():
is_abundant(20)
is_abundant(11)
is_abundant(104)
Algorithm Explanation
- Create a function
divisor_sum(number)
that calculates the sum of the proper divisors of the given number. Iterate through numbers from 1 tonumber/2
. If the current number dividesnumber
evenly (i.e.,number % i == 0
), add it to the result. - Create a function
is_abundant(number)
that determines whether a number is abundant or not. Call thedivisor_sum
function to get the sum of proper divisors. Compare the sum with the number itself. If the sum is greater, print that the number is an abundant number; otherwise, print that it is not. - In the
main()
function, callis_abundant
for different test cases.
Code Solution
//C Program
//Check if a given number is abundant or not
#include <stdio.h>
// Function which is calculate sum of divisors in given number
int divisor_sum(int number)
{
int result = 0;
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 abundant or not
void is_abundant(int number)
{
//Check whether number divisor sum is greater than given number?
if(divisor_sum(number) > number)
{
//When Yes
printf("%d Is an Abundant Number\n",number);
}
else
{
//When No
printf("%d Is not a Abundant Number\n",number);
}
}
int main() {
//Test Cases
is_abundant(20);
is_abundant(11);
is_abundant(104);
return 0;
}
Output
20 Is an Abundant Number
11 Is not a Abundant Number
104 Is an Abundant Number
/*
C++ Program
Check if a given number is abundant 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) {
int result = 0;
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 abundant or not
void is_abundant(int number) {
//Check whether number divisor sum is greater than given number?
if (this->divisor_sum(number) > number) {
//When Yes
cout << number << " Abundant Number\n";
} else {
//When No
cout << number << " Is not a Abundant Number\n";
}
}
};
int main() {
MyNumber obj ;
// Test Case
obj.is_abundant(20);
obj.is_abundant(11);
obj.is_abundant(104);
return 0;
}
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant Number
/*
Java Program
Check if a given number is abundant or not
*/
public class MyNumber {
// Function which is calculate sum of divisors in given number
public int divisor_sum(int number)
{
int result = 0;
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 abundant or not
public void is_abundant(int number)
{
//Check whether number divisor sum is greater than given number?
if(divisor_sum(number) > number)
{
//When Yes
System.out.print(number+" Abundant Number\n");
}
else
{
//When No
System.out.print(number+" Is not a Abundant Number\n");
}
}
public static void main(String[] args) {
MyNumber obj = new MyNumber();
// Test Case
obj.is_abundant(20);
obj.is_abundant(11);
obj.is_abundant(104);
}
}
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant Number
/*
C# Program
Check if a given number is abundant or not
*/
using System;
public class MyNumber {
// Function which is calculate sum of divisors in given number
public int divisor_sum(int number) {
int result = 0;
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 abundant or not
public void is_abundant(int number) {
//Check whether number divisor sum is greater than given number?
if (divisor_sum(number) > number) {
//When Yes
Console.Write(number + " Abundant Number\n");
} else {
//When No
Console.Write(number + " Is not a Abundant Number\n");
}
}
public static void Main(String[] args) {
MyNumber obj = new MyNumber();
// Test Case
obj.is_abundant(20);
obj.is_abundant(11);
obj.is_abundant(104);
}
}
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant Number
# Python 3 Program
# Java Program
class MyNumber :
# Function which is calculate sum of divisors in given number
def divisor_sum(self, number) :
result = 0
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 abundant or not
def is_abundant(self, number) :
#Check whether number divisor sum is greater than given number?
if (self.divisor_sum(number) > number) :
print(number ," Abundant Number")
else :
print(number ," Is not a Abundant Number")
def main() :
obj = MyNumber()
obj.is_abundant(20)
obj.is_abundant(11)
obj.is_abundant(104)
if __name__ == "__main__":
main()
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant Number
# Ruby Program
# Check if a given number is abundant or not
class MyNumber
# Function which is calculate sum of divisors in given number
def divisor_sum(number)
result = 0
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 abundant or not
def is_abundant(number)
#Check whether number divisor sum is greater than given number?
if (self.divisor_sum(number) > number)
print(number ," Abundant Number\n")
else
print(number ," Is not a Abundant Number\n")
end
end
end
def main()
obj = MyNumber.new()
obj.is_abundant(20)
obj.is_abundant(11)
obj.is_abundant(104)
end
main()
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant Number
/*
Scala Program
Check if a given number is abundant or not
*/
class MyNumber {
// Function which is calculate sum of divisors in given number
def divisor_sum(number: Int): Int = {
var result: Int = 0;
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 abundant or not
def is_abundant(number: Int): Unit = {
//Check whether number divisor sum is greater than given number?
if (this.divisor_sum(number) > number) {
print(s"$number Abundant Number\n");
} else {
print(s"$number Is not a Abundant Number\n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
obj.is_abundant(20);
obj.is_abundant(11);
obj.is_abundant(104);
}
}
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant Number
/*
Swift 4 Program
Check if a given number is abundant or not
*/
class MyNumber {
// Function which is calculate sum of divisors in given number
func divisor_sum(_ number: Int) -> Int {
var result: Int = 0;
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 abundant or not
func is_abundant(_ number: Int) {
//Check whether number divisor sum is greater than given number?
if (self.divisor_sum(number) > number) {
print(number ," Abundant Number");
} else {
print(number ," Is not a Abundant Number");
}
}
}
func main() {
let obj: MyNumber = MyNumber();
obj.is_abundant(20);
obj.is_abundant(11);
obj.is_abundant(104);
}
main();
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant Number
<?php
/*
Php Program
Check if a given number is abundant or not
*/
class MyNumber {
// Function which is calculate sum of divisors in given number
public function divisor_sum($number) {
$result = 0;
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 abundant or not
public function is_abundant($number) {
//Check whether number divisor sum is greater than given number?
if ($this->divisor_sum($number) > $number) {
//When Yes
echo($number ." Abundant Number\n");
} else {
//When No
echo($number ." Is not a Abundant Number\n");
}
}
};
function main() {
$obj = new MyNumber();
// Test Case
$obj->is_abundant(20);
$obj->is_abundant(11);
$obj->is_abundant(104);
}
main();
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant Number
/*
Node Js Program
Check if a given number is abundant or not
*/
class MyNumber {
// Function which is calculate sum of divisors in given number
divisor_sum(number) {
var result = 0;
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 abundant or not
is_abundant(number) {
//Check whether number divisor sum is greater than given number?
if (this.divisor_sum(number) > number) {
//When Yes
process.stdout.write(number + " Abundant Number\n");
} else {
//When No
process.stdout.write(number + " Is not a Abundant Number\n");
}
}
}
function main(args) {
var obj = new MyNumber();
// Test Case
obj.is_abundant(20);
obj.is_abundant(11);
obj.is_abundant(104)
}
main();
Output
20 Abundant Number
11 Is not a Abundant Number
104 Abundant 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_abundant
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.
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.
New Comment