给定一个数字N ,任务是找到所有已婚夫妇的数字之和,直到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.
例子:
Input: 78
Output: 123
Explanation: 48 and 75 is the only pair of Betrothed Numbers upto 78.
Input: 5
Output: 0
Explanation: There is no pair of Betrothed Numbers up to 50.
方法:
- 初始化数组以存储所有订婚的数字,直到N
- 使用所有适当除数的和来找到已婚夫妇的数字,并将该对存储在数组中。
- 然后找到的总和小于所有的数字和等于n。
- 结果总和为所需值。
下面的代码是上述方法的实现:
C++
// C++ program to find the sum of the
// all betrothed numbers up to N
#include
using namespace std;
// Function to find the sum of
// the all betrothed numbers
int Betrothed_Sum(int n)
{
// To store the betrothed
// numbers
vector 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
Java
// 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 Set = new Vector();
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
Python3
# 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))
C#
// C# program to find the sum
// of the all betrothed numbers
// up to N
using System;
using System.Collections;
class GFG{
// Function to find the sum of
// the all betrothed numbers
public static int Betrothed_Sum(int n)
{
// To store the betrothed
// numbers
ArrayList set = new ArrayList();
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.Count; i++)
{
if ((int)set[i] <= n)
Summ += (int)set[i];
}
return Summ;
}
// Driver code
static public void Main()
{
int n = 78;
Console.WriteLine(Betrothed_Sum(n));
}
}
// This code is contributed by offbeat
Javascript
输出:
123
时间复杂度: O(N * sqrt(N))