Count the pairs of amicable numbers
Amicable numbers are two different positive integers, where each number is the sum of the proper divisors of the other number. In other words, let's say we have two numbers a and b, then a is the sum of the proper divisors of b, and b is the sum of the proper divisors of a.
For example, the smallest pair of amicable numbers is (220, 284). The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110. Adding them up gives us 284. The proper divisors of 284 are 1, 2, 4, 71, and 142. Adding them up gives us 220.
Amicable numbers have been known since ancient times, and they have fascinated mathematicians for centuries. There are infinitely many pairs of amicable numbers, but they become increasingly rare as the numbers get larger.
Here given code implementation process.
/*
Java Program
Count the pairs of amicable numbers
*/
import java.util.HashSet;
public class AmicableNumbers
{
// Find the sum of divisors of given value
public int divisorSum(int value)
{
int sum = 1;
for (int i = 2; i <= Math.sqrt(value); ++i)
{
// Check whether value is divisible by (i) or not
if (value % i == 0)
{
if ((value / i) != i)
{
sum += (value / i) + i;
}
else
{
sum += i;
}
}
}
// Return calculated result
return sum;
}
public void amicablePair(int[] arr, int n)
{
// Use to get unique element
HashSet < Integer > record = new HashSet < Integer > ();
// Define auxiliary variables
int result = 0;
int info = 0;
int i = 0;
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
// Get unique values in array
record.add(arr[i]);
}
// Count the number of amicable pair
for (i = 0; i < n; i++)
{
info = divisorSum(arr[i]);
if (record.contains(info) && divisorSum(info) == arr[i])
{ // Whe pair is amicable
result++;
}
}
if (result != 0)
{
result = result / 2;
// Display calculated result
System.out.println(" Output : " + result);
}
else
{
System.out.println(" None \n");
}
}
public static void main(String[] arg)
{
AmicableNumbers task = new AmicableNumbers();
// Define array of integer elements
int[] arr = {
30 , 1184 , 66992 , 1210 , 5020 , 6232 ,
10856 , 6368 , 10744 , 12285 , 14595 , 17296 ,
18416 , 63020 , 2924 , 100 , 76084 , 5564 ,
66928 , 600 , 2620, 313
};
// Get the number of elements
int n = arr.length;
// Test
// Amicable Pairs
// (1184, 1210), (2620, 2924),
// (5020, 5564), (6232, 6368),
// (10744, 10856), (12285, 14595),
// (17296, 18416), (63020, 76084),
// (66928, 66992).
task.amicablePair(arr, n);
}
}
Output
Output : 9
// Include header file
#include <iostream>
#include <math.h>
#include <set>
using namespace std;
/*
C++ Program
Count the pairs of amicable numbers
*/
class AmicableNumbers
{
public:
// Find the sum of divisors of given value
int divisorSum(int value)
{
// Return calculated result
int sum = 1;
for (int i = 2; i <= sqrt(value); ++i)
{
// Check whether value is divisible by (i) or not
if (value % i == 0)
{
if ((value / i) != i)
{
sum += (value / i) + i;
}
else
{
sum += i;
}
}
}
return sum;
}
void amicablePair(int arr[], int n)
{
// Use to get unique element
set < int > record;
// Define auxiliary variables
int result = 0;
int info = 0;
int i = 0;
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
// Get unique values in array
record.insert(arr[i]);
}
// Count the number of amicable pair
for (i = 0; i < n; i++)
{
info = this->divisorSum(arr[i]);
if (record.find(info) != record.end()
&& this->divisorSum(info) == arr[i])
{
// Whe pair is amicable
result++;
}
}
if (result != 0)
{
result = result / 2;
// Display calculated result
cout << " Output : " << result;
}
else
{
cout << " None \n";
}
}
};
int main()
{
AmicableNumbers task = AmicableNumbers();
// Define array of integer elements
int arr[] = {
30 , 1184 , 66992 , 1210 , 5020 , 6232 ,
10856 , 6368 , 10744 , 12285 , 14595 ,
17296 , 18416 , 63020 , 2924 , 100 ,
76084 , 5564 , 66928 , 600 , 2620 , 313
};
// Get the number of elements
int n = sizeof(arr) / sizeof(arr[0]);
// Test
// Amicable Pairs
// (1184, 1210) (2620, 2924)
// (5020, 5564) (6232, 6368)
// (10744, 10856) (12285, 14595)
// (17296, 18416) (63020, 76084)
// (66928, 66992)
task.amicablePair(arr, n);
return 0;
}
Output
Output : 9
// Include namespace system
using System;
using System.Collections.Generic;
/*
C# Program
Count the pairs of amicable numbers
*/
public class AmicableNumbers
{
// Find the sum of divisors of given value
public int divisorSum(int value)
{
// Return calculated result
int sum = 1;
for (int i = 2; i <= Math.Sqrt(value); ++i)
{
// Check whether value is divisible by (i) or not
if (value % i == 0)
{
if ((value / i) != i)
{
sum += (value / i) + i;
}
else
{
sum += i;
}
}
}
return sum;
}
public void amicablePair(int[] arr, int n)
{
// Use to get unique element
HashSet < int > record = new HashSet < int > ();
// Define auxiliary variables
int result = 0;
int info = 0;
int i = 0;
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
// Get unique values in array
record.Add(arr[i]);
}
// Count the number of amicable pair
for (i = 0; i < n; i++)
{
info = divisorSum(arr[i]);
if (record.Contains(info) &&
divisorSum(info) == arr[i])
{
// Whe pair is amicable
result++;
}
}
if (result != 0)
{
result = result / 2;
// Display calculated result
Console.WriteLine(" Output : " + result);
}
else
{
Console.WriteLine(" None \n");
}
}
public static void Main(String[] arg)
{
AmicableNumbers task = new AmicableNumbers();
// Define array of integer elements
int[] arr = {
30 , 1184 , 66992 , 1210 , 5020 , 6232 ,
10856 , 6368 , 10744 , 12285 , 14595 ,
17296 , 18416 , 63020 , 2924 , 100 ,
76084 , 5564 , 66928 , 600 , 2620 , 313
};
// Get the number of elements
int n = arr.Length;
// Test
// Amicable Pairs
// (1184, 1210) (2620, 2924)
// (5020, 5564) (6232, 6368)
// (10744, 10856) (12285, 14595)
// (17296, 18416) (63020, 76084)
// (66928, 66992)
task.amicablePair(arr, n);
}
}
Output
Output : 9
<?php
/*
Php Program
Count the pairs of amicable numbers
*/
class AmicableNumbers
{
// Find the sum of divisors of given value
public function divisorSum($value)
{
// Return calculated result
$sum = 1;
for ($i = 2; $i <= sqrt($value); ++$i)
{
// Check whether value is divisible by (i) or not
if ($value % $i == 0)
{
if ((intval($value / $i)) != $i)
{
$sum += (intval($value / $i)) + $i;
}
else
{
$sum += $i;
}
}
}
return $sum;
}
public function amicablePair( & $arr, $n)
{
// Use to get unique element
$record = array();
// Define auxiliary variables
$result = 0;
$info = 0;
$i = 0;
// iterate the loop through by size
for ($i = 0; $i < $n; ++$i)
{
$record[] = $arr[$i];
}
// Count the number of amicable pair
for ($i = 0; $i < $n; $i++)
{
$info = $this->divisorSum($arr[$i]);
if (in_array($info, $record, TRUE)
&& $this->divisorSum($info) == $arr[$i])
{
// Whe pair is amicable
$result++;
}
}
if ($result != 0)
{
$result = intval($result / 2);
// Display calculated result
echo " Output : ". $result;
}
else
{
echo " None \n";
}
}
}
function main()
{
$task = new AmicableNumbers();
// Define array of integer elements
$arr = array(30, 1184, 66992, 1210, 5020,
6232, 10856, 6368, 10744,
12285, 14595, 17296, 18416,
63020, 2924, 100, 76084,
5564, 66928, 600, 2620, 313);
// Get the number of elements
$n = count($arr);
$task->amicablePair($arr, $n);
}
main();
Output
Output : 9
/*
Node Js Program
Count the pairs of amicable numbers
*/
class AmicableNumbers
{
// Find the sum of divisors of given value
divisorSum(value)
{
// Return calculated result
var sum = 1;
for (var i = 2; i <= Math.sqrt(value); ++i)
{
// Check whether value is divisible by (i) or not
if (value % i == 0)
{
if ((parseInt(value / i)) != i)
{
sum += (parseInt(value / i)) + i;
}
else
{
sum += i;
}
}
}
return sum;
}
amicablePair(arr, n)
{
// Use to get unique element
var record = new Set();
// Define auxiliary variables
var result = 0;
var info = 0;
var i = 0;
// iterate the loop through by size
for (i = 0; i < n; ++i)
{
// Get unique values in array
record.add(arr[i]);
}
// Count the number of amicable pair
for (i = 0; i < n; i++)
{
info = this.divisorSum(arr[i]);
if (record.has(info)
&& this.divisorSum(info) == arr[i])
{
// Whe pair is amicable
result++;
}
}
if (result != 0)
{
result = parseInt(result / 2);
// Display calculated result
process.stdout.write(" Output : " + result);
}
else
{
process.stdout.write(" None \n");
}
}
}
function main()
{
var task = new AmicableNumbers();
// Define array of integer elements
var arr = [30, 1184, 66992, 1210, 5020, 6232,
10856, 6368, 10744, 12285, 14595,
17296, 18416, 63020, 2924, 100,
76084, 5564, 66928, 600, 2620, 313];
// Get the number of elements
var n = arr.length;
// Test
// Amicable Pairs
// (1184, 1210) (2620, 2924)
// (5020, 5564) (6232, 6368)
// (10744, 10856) (12285, 14595)
// (17296, 18416) (63020, 76084)
// (66928, 66992)
task.amicablePair(arr, n);
}
main();
Output
Output : 9
import math
# Python 3 Program
# Count the pairs of amicable numbers
class AmicableNumbers :
# Find the sum of divisors of given value
def divisorSum(self, value) :
# Return calculated result
sum = 1
i = 2
while (i <= math.sqrt(value)) :
# Check whether value is divisible by (i) or not
if (value % i == 0) :
if ((int(value / i)) != i) :
sum += (int(value / i)) + i
else :
sum += i
i += 1
return sum
def amicablePair(self, arr, n) :
# Use to get unique element
record = set()
# Define auxiliary variables
result = 0
info = 0
i = 0
# iterate the loop through by size
while (i < n) :
# Get unique values in list
record.add(arr[i])
i += 1
i = 0
# Count the number of amicable pair
while (i < n) :
info = self.divisorSum(arr[i])
if (info in record and self.divisorSum(info) == arr[i]) :
# Whe pair is amicable
result += 1
i += 1
if (result != 0) :
result = int(result / 2)
# Display calculated result
print(" Output : ", result, end = "")
else :
print(" None ")
def main() :
task = AmicableNumbers()
# Define list of integer elements
arr = [30, 1184, 66992, 1210, 5020,
6232, 10856, 6368, 10744, 12285,
14595, 17296, 18416, 63020, 2924,
100, 76084, 5564, 66928, 600, 2620, 313]
# Get the number of elements
n = len(arr)
# Test
# Amicable Pairs
# (1184, 1210) (2620, 2924)
# (5020, 5564) (6232, 6368)
# (10744, 10856) (12285, 14595)
# (17296, 18416) (63020, 76084)
# (66928, 66992)
task.amicablePair(arr, n)
if __name__ == "__main__": main()
Output
Output : 9
require 'set'
#
# Ruby Program
# Count the pairs of amicable numbers
class AmicableNumbers
# Find the sum of divisors of given value
def divisorSum(value)
# Return calculated result
sum = 1
i = 2
while (i <= Math.sqrt(value))
# Check whether value is divisible by (i) or not
if (value % i == 0)
if ((value / i) != i)
sum += (value / i) + i
else
sum += i
end
end
i += 1
end
return sum
end
def amicablePair(arr, n)
# Use to get unique element
record = Set[]
# Define auxiliary variables
result = 0
info = 0
i = 0
# iterate the loop through by size
while (i < n)
record.add(arr[i])
i += 1
end
i = 0
# Count the number of amicable pair
while (i < n)
info = self.divisorSum(arr[i])
if (record.include?(info) &&
self.divisorSum(info) == arr[i])
# Whe pair is amicable
result += 1
end
i += 1
end
if (result != 0)
result = result / 2
# Display calculated result
print(" Output : ", result)
else
print(" None \n")
end
end
end
def main()
task = AmicableNumbers.new()
# Define array of integer elements
arr = [30, 1184, 66992, 1210, 5020, 6232,
10856, 6368, 10744, 12285, 14595,
17296, 18416, 63020, 2924, 100,
76084, 5564, 66928, 600, 2620, 313]
# Get the number of elements
n = arr.length
# Test
# Amicable Pairs
# (1184, 1210) (2620, 2924)
# (5020, 5564) (6232, 6368)
# (10744, 10856) (12285, 14595)
# (17296, 18416) (63020, 76084)
# (66928, 66992)
task.amicablePair(arr, n)
end
main()
Output
Output : 9
import scala.collection.mutable._;
/*
Scala Program
Count the pairs of amicable numbers
*/
class AmicableNumbers
{
// Find the sum of divisors of given value
def divisorSum(value: Int): Int = {
// Return calculated result
var sum: Int = 1;
var i: Int = 2;
while (i <= Math.sqrt(value))
{
// Check whether value is divisible by (i) or not
if (value % i == 0)
{
if (((value / i).toInt) != i)
{
sum += ((value / i).toInt) + i;
}
else
{
sum += i;
}
}
i += 1;
}
return sum;
}
def amicablePair(arr: Array[Int], n: Int): Unit = {
// Use to get unique element
var record: Set[Int] = Set();
// Define auxiliary variables
var result: Int = 0;
var info: Int = 0;
var i: Int = 0;
// iterate the loop through by size
while (i < n)
{
// Get unique values in array
record.add(arr(i));
i += 1;
}
i = 0;
// Count the number of amicable pair
while (i < n)
{
info = this.divisorSum(arr(i));
if (record.contains(info)
&& this.divisorSum(info) == arr(i))
{
// Whe pair is amicable
result += 1;
}
i += 1;
}
if (result != 0)
{
result = (result / 2).toInt;
// Display calculated result
print(" Output : " + result);
}
else
{
print(" None \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: AmicableNumbers = new AmicableNumbers();
// Define array of integer elements
var arr: Array[Int] = Array(
30, 1184, 66992, 1210, 5020,
6232, 10856, 6368, 10744, 12285,
14595, 17296, 18416, 63020, 2924,
100, 76084, 5564, 66928, 600, 2620, 313);
// Get the number of elements
var n: Int = arr.length;
// Test
// Amicable Pairs
// (1184, 1210) (2620, 2924)
// (5020, 5564) (6232, 6368)
// (10744, 10856) (12285, 14595)
// (17296, 18416) (63020, 76084)
// (66928, 66992)
task.amicablePair(arr, n);
}
}
Output
Output : 9
import Foundation
/*
Swift 4 Program
Count the pairs of amicable numbers
*/
class AmicableNumbers
{
// Find the sum of divisors of given value
func divisorSum(_ value: Int)->Int
{
// Return calculated result
var sum: Int = 1;
var i: Int = 2;
while (i <= Int(sqrt(Double(value))))
{
// Check whether value is divisible by (i) or not
if (value % i == 0)
{
if ((value / i) != i)
{
sum += (value / i) + i;
}
else
{
sum += i;
}
}
i += 1;
}
return sum;
}
func amicablePair(_ arr: [Int], _ n: Int)
{
// Use to get unique element
var record = Set<Int>()
// Define auxiliary variables
var result: Int = 0;
var info: Int = 0;
var i: Int = 0;
// iterate the loop through by size
while (i < n)
{
// Get unique values in array
record.insert(arr[i]);
i += 1;
}
i = 0;
// Count the number of amicable pair
while (i < n)
{
info = self.divisorSum(arr[i]);
if (record.contains(info)
&& self.divisorSum(info) == arr[i])
{
// Whe pair is amicable
result += 1;
}
i += 1;
}
if (result != 0)
{
result = result / 2;
// Display calculated result
print(" Output : ", result, terminator: "");
}
else
{
print(" None ");
}
}
}
func main()
{
let task: AmicableNumbers = AmicableNumbers();
// Define array of integer elements
let arr: [Int] = [30, 1184, 66992, 1210, 5020, 6232,
10856, 6368, 10744, 12285, 14595,
17296, 18416, 63020, 2924, 100,
76084, 5564, 66928, 600, 2620, 313];
// Get the number of elements
let n: Int = arr.count;
// Test
// Amicable Pairs
// (1184, 1210) (2620, 2924)
// (5020, 5564) (6232, 6368)
// (10744, 10856) (12285, 14595)
// (17296, 18416) (63020, 76084)
// (66928, 66992)
task.amicablePair(arr, n);
}
main();
Output
Output : 9
/*
Kotlin Program
Count the pairs of amicable numbers
*/
class AmicableNumbers
{
// Find the sum of divisors of given value
fun divisorSum(value: Int): Int
{
// Return calculated result
var sum: Int = 1;
var i: Int = 2;
while (i <= Math.sqrt(value.toDouble()))
{
// Check whether value is divisible by (i) or not
if (value % i == 0)
{
if ((value / i) != i)
{
sum += (value / i) + i;
}
else
{
sum += i;
}
}
i += 1;
}
return sum;
}
fun amicablePair(arr: Array < Int > , n: Int): Unit
{
// Use to get unique element
var record: MutableSet <Int> = mutableSetOf <Int> ();
// Define auxiliary variables
var result: Int = 0;
var info: Int ;
var i: Int = 0;
// iterate the loop through by size
while (i < n)
{
// Get unique values in array
record.add(arr[i]);
i += 1;
}
i = 0;
// Count the number of amicable pair
while (i < n)
{
info = this.divisorSum(arr[i]);
if (record.contains(info)
&& this.divisorSum(info) == arr[i])
{
// Whe pair is amicable
result += 1;
}
i += 1;
}
if (result != 0)
{
result = result / 2;
// Display calculated result
print(" Output : " + result);
}
else
{
print(" None \n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: AmicableNumbers = AmicableNumbers();
// Define array of integer elements
var arr: Array < Int > = arrayOf(30, 1184, 66992, 1210, 5020, 6232, 10856, 6368, 10744, 12285, 14595, 17296, 18416, 63020, 2924, 100, 76084, 5564, 66928, 600, 2620, 313);
// Get the number of elements
var n: Int = arr.count();
// Test
// Amicable Pairs
// (1184, 1210) (2620, 2924)
// (5020, 5564) (6232, 6368)
// (10744, 10856) (12285, 14595)
// (17296, 18416) (63020, 76084)
// (66928, 66992)
task.amicablePair(arr, n);
}
Output
Output : 9
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