Find the sum of all Betrothed numbers up to N

Given a number N, the task is to find the sum of the all Betrothed numbers up to N.

Let, A and B are a Betrothed number pair, then the sum of the proper divisors of A is equal to B+1 and the sum of the proper divisors of B is equal to A+1.

Examples: 

Input: 78 
Output: 123 
Explanation: 48 and 75 is the only pair of Betrothed Numbers upto 78.

Input:
Output:
Explanation: There is no pair of Betrothed Numbers up to 50. 



Approach: 

  • Initialise the array to store all Betrothed numbers upto to N
  • Find the Betrothed numbers using sum of all its proper divisors and store the pair in the array.
  • Then find the sum all the numbers less than and equal to N.
  • The resultant sum is the required value.

Below code is the implementation of the above approach: 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the sum of the  
// all betrothed numbers up to N
#include<bits/stdc++.h>
using namespace std;
 
// Function to find the sum of
// the all betrothed numbers
int Betrothed_Sum(int n)
{
    // To store the betrothed
    // numbers
    vector<int > Set;
     
    for(int number_1 = 1; number_1 < n;
                            number_1++)
    {
         
       // Calculate sum of
       // number_1's divisors
       // 1 is always a divisor
       int sum_divisor_1 = 1;
        
       // i = 2 because we don't
       // want to include
       // 1 as a divisor.
       int i = 2;
        
       while (i * i <= number_1)
       {
           if (number_1 % i == 0)
           {
               sum_divisor_1 = sum_divisor_1 + i;
                
               if (i * i != number_1)
                   sum_divisor_1 += number_1 / i;
           }
           i ++;
       }
       if (sum_divisor_1 > number_1)
       {
           int number_2 = sum_divisor_1 - 1;
           int sum_divisor_2 = 1;
           int j = 2;
            
           while (j * j <= number_2)
           {
               if (number_2 % j == 0)
               {
                   sum_divisor_2 += j;
                   if (j * j != number_2)
                       sum_divisor_2 += number_2 / j;
               }
               j = j + 1;
           }
           if (sum_divisor_2 == number_1 + 1 and
               number_1 <= n && number_2 <= n)
           {
               Set.push_back(number_1);
               Set.push_back(number_2);
           }
       }
    }
     
    // Sum all betrothed
    // numbers up to N
    int Summ = 0;
    for(auto i : Set)
    {
       if(i <= n)
          Summ += i;
    }
    return Summ;
}
 
// Driver code
int main()
{
    int n = 78;
     
    cout << Betrothed_Sum(n);
    return 0;
}
 
// This code is contributed by ishayadav181

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the sum
// of the all betrothed numbers
// up to N 
import java.util.*;
 
class GFG{
     
// Function to find the sum of 
// the all betrothed numbers 
public static int Betrothed_Sum(int n)
{
     
    // To store the betrothed 
    // numbers 
    Vector<Integer> Set = new Vector<Integer>();
       
    for(int number_1 = 1;
            number_1 < n; 
            number_1++)
    {
         
       // Calculate sum of 
       // number_1's divisors 
       // 1 is always a divisor
       int sum_divisor_1 = 1;
          
       // i = 2 because we don't 
       // want to include 
       // 1 as a divisor. 
       int i = 2;
          
       while (i * i <= number_1)
       {
           if (number_1 % i == 0)
           {
               sum_divisor_1 = sum_divisor_1 + i;
                  
               if (i * i != number_1)
                   sum_divisor_1 += number_1 / i; 
           }
           i ++;
       }
        
       if (sum_divisor_1 > number_1)
       {
           int number_2 = sum_divisor_1 - 1;
           int sum_divisor_2 = 1;
           int j = 2;
              
           while (j * j <= number_2) 
           {
               if (number_2 % j == 0
               {
                   sum_divisor_2 += j;
                    
                   if (j * j != number_2)
                       sum_divisor_2 += number_2 / j;
               }
               j = j + 1;
           }
           if (sum_divisor_2 == number_1 + 1 && 
               number_1 <= n && number_2 <= n)
           {
               Set.add(number_1);
               Set.add(number_2);
           }
       }
    }
       
    // Sum all betrothed 
    // numbers up to N 
    int Summ = 0;
    for(int i = 0; i < Set.size(); i++)
    {
       if (Set.get(i) <= n) 
          Summ += Set.get(i);
    }
    return Summ;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 78;
     
    System.out.println(Betrothed_Sum(n));
}
}
 
// This code is contributed by divyeshrabadiya07

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the
# Sum of the all Betrothed
# numbers up to N
 
import math
 
# Function to find the Sum of
# the all Betrothed numbers
def Betrothed_Sum(n):
     
    # To store the Betrothed
    # numbers
    Set = []
 
    for number_1 in range(1, n):
 
        # Calculate sum of
        # number_1's divisors
 
        # 1 is always a divisor
        sum_divisor_1 = 1
 
        # i = 2 because we don't
        # want to include
        # 1 as a divisor.
        i = 2
 
        while i * i <= number_1:
            if (number_1 % i == 0):
                sum_divisor_1 = sum_divisor_1 + i
 
                if (i * i != number_1):
                    sum_divisor_1 += number_1 // i
            i = i + 1
 
        if (sum_divisor_1 > number_1):
 
            number_2 = sum_divisor_1 - 1
            sum_divisor_2 = 1
            j = 2
            while j * j <= number_2:
                if (number_2 % j == 0):
                    sum_divisor_2 += j
                    if (j * j != number_2):
                        sum_divisor_2 += number_2 // j
                j = j + 1
 
            if (sum_divisor_2 == number_1 + 1
                and number_1<= n and number_2<= n):
                Set.append(number_1)
                Set.append(number_2)
 
    # Sum all Betrothed
    # numbers up to N
    Summ = 0
    for i in Set:
        if i <= n:
            Summ += i
    return Summ
 
 
# Driver Code
n = 78
print(Betrothed_Sum(n))

chevron_right


Output: 

123

 

Time Complexity: O(N * sqrt(N))
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.