Betrothed numbers are two positive numbers such that the sum of the proper divisors of either number is one more than (or one plus) the value of the other number. Our task is to find these pairs efficiently.
Example :
(48, 75) is an example of Betrothed numbers
Divisors of 48 : 1, 2, 3, 4, 6, 8, 12,
16, 24. Their sum is 76.
Divisors of 75 : 1, 3, 5, 15, 25. Their
sum is 49.
Given a positive integer n, print all Brothered numbers (which is a pair) such that one of the numbers in every pair is smaller than n.
Example :
Input : n = 1000
Output : (48, 75), (140, 195)
Input : n = 10000
Output : (48, 75), (140, 195), (1050, 1925)
(1575, 1648), (2024, 2295), (5775,
6128) (8892, 16587), (9504, 20735)
The idea used in below program is simple. We traverse through all numbers from 1 to n-1. For every number num1, we find sum of its proper divisors say sum1. After finding sum1, we check if the number num2 = sum1 + 1 which has sum of divisors as num1 + 1
C++
#include <iostream>
using namespace std;
void BetrothedNumbers( int n)
{
for ( int num1 = 1; num1 < n; num1++) {
int sum1 = 1;
for ( int i = 2; i * i <= num1; i++)
{
if (num1 % i == 0) {
sum1 += i;
if (i * i != num1)
sum1 += num1 / i;
}
}
if (sum1 > num1)
{
int num2 = sum1 - 1;
int sum2 = 1;
for ( int j = 2; j * j <= num2; j++)
{
if (num2 % j == 0) {
sum2 += j;
if (j * j != num2)
sum2 += num2 / j;
}
}
if (sum2 == num1+1)
printf ( "(%d, %d)\n" , num1, num2);
}
}
}
int main()
{
int n = 10000;
BetrothedNumbers(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void BetrothedNumbers( int n)
{
for ( int num1 = 1 ; num1 < n; num1++) {
int sum1 = 1 ;
for ( int i = 2 ; i * i <= num1; i++)
{
if (num1 % i == 0 ) {
sum1 += i;
if (i * i != num1)
sum1 += num1 / i;
}
}
if (sum1 > num1)
{
int num2 = sum1 - 1 ;
int sum2 = 1 ;
for ( int j = 2 ; j * j <= num2; j++)
{
if (num2 % j == 0 ) {
sum2 += j;
if (j * j != num2)
sum2 += num2 / j;
}
}
if (sum2 == num1+ 1 )
System.out.println( "(" + num1 +
", " + num2 + ")" );
}
}
}
public static void main(String args[])
{
int n = 10000 ;
BetrothedNumbers(n);
}
}
|
Python
def BetrothedNumbers(n) :
for num1 in range ( 1 ,n) :
sum1 = 1
i = 2
while i * i < = num1 :
if (num1 % i = = 0 ) :
sum1 = sum1 + i
if (i * i ! = num1) :
sum1 + = num1 / i
i = i + 1
if (sum1 > num1) :
num2 = sum1 - 1
sum2 = 1
j = 2
while j * j < = num2 :
if (num2 % j = = 0 ) :
sum2 + = j
if (j * j ! = num2) :
sum2 + = num2 / j
j = j + 1
if (sum2 = = num1 + 1 ) :
print ( '(' + str (num1) + ', ' + str (num2) + ')' )
n = 10000
BetrothedNumbers(n)
|
C#
using System;
class GFG
{
static void BetrothedNumbers( int n)
{
for ( int num1 = 1; num1 < n; num1++)
{
int sum1 = 1;
for ( int i = 2; i * i <= num1; i++)
{
if (num1 % i == 0)
{
sum1 += i;
if (i * i != num1)
sum1 += num1 / i;
}
}
if (sum1 > num1)
{
int num2 = sum1 - 1;
int sum2 = 1;
for ( int j = 2; j * j <= num2; j++)
{
if (num2 % j == 0)
{
sum2 += j;
if (j * j != num2)
sum2 += num2 / j;
}
}
if (sum2 == num1 + 1)
Console.WriteLine( "(" + num1 +
", " + num2 + ")" );
}
}
}
public static void Main()
{
int n = 10000;
BetrothedNumbers(n);
}
}
|
PHP
<?php
function BetrothedNumbers( $n )
{
for ( $num1 = 1; $num1 < $n ; $num1 ++) {
$sum1 = 1;
for ( $i = 2; $i * $i <= $num1 ; $i ++)
{
if ( $num1 % $i == 0) {
$sum1 += $i ;
if ( $i * $i != $num1 )
$sum1 += $num1 / $i ;
}
}
if ( $sum1 > $num1 )
{
$num2 = $sum1 - 1;
$sum2 = 1;
for ( $j = 2; $j * $j <= $num2 ; $j ++)
{
if ( $num2 % $j == 0) {
$sum2 += $j ;
if ( $j * $j != $num2 )
$sum2 += $num2 / $j ;
}
}
if ( $sum2 == $num1 +1)
echo "(" , $num1 , " " , $num2 , ")\n" ;
}
}
}
$n = 10000;
BetrothedNumbers( $n );
?>
|
Javascript
<script>
function BetrothedNumbers(n)
{
for (let num1 = 1; num1 < n; num1++)
{
let sum1 = 1;
for (let i = 2; i * i <= num1; i++)
{
if (num1 % i == 0) {
sum1 += i;
if (i * i != num1)
sum1 += parseInt(num1 / i);
}
}
if (sum1 > num1)
{
let num2 = sum1 - 1;
let sum2 = 1;
for (let j = 2; j * j <= num2; j++)
{
if (num2 % j == 0) {
sum2 += j;
if (j * j != num2)
sum2 += parseInt(num2 / j);
}
}
if (sum2 == (num1+1))
document.write(`(${num1}, ${num2})<br>`);
}
}
}
let n = 10000;
BetrothedNumbers(n);
</script>
|
Output :
(48, 75)
(140, 195)
(1050, 1925)
(1575, 1648)
(2024, 2295)
(5775, 6128)
(8892, 16587)
(9504, 20735)
Time Complexity: O(n?n)
Auxiliary Space: O(1)
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...