给定正整数N ,任务是找到的值其中函数F(x)可以定义为’ x ‘的所有适当除数的和。
例子:
Input: N = 4
Output: 5
Explanation:
Sum of all proper divisors of numbers:
F(1) = 0
F(2) = 1
F(3) = 1
F(4) = 1 + 2 = 3
Total Sum = F(1) + F(2) + F(3) + F(4) = 0 + 1 + 1 + 3 = 5
Input: N = 5
Output: 6
Explanation:
Sum of all proper divisors of numbers:
F(1) = 0
F(2) = 1
F(3) = 1
F(4) = 1 + 2 = 3
F(5) = 1
Total Sum = F(1) + F(2) + F(3) + F(4) + F(5) = 0 + 1 + 1 + 3 + 1 = 6
幼稚的方法:想法是分别找到[1,N]范围内每个数字的适当除数的总和,然后将它们相加以找到所需的总和。
下面是上述方法的实现:
C++
// C++ implemenation to find sum of all
// proper divisor of number up to N
#include
using namespace std;
// Utility function to find sum of
// all proper divisor of number up to N
int properDivisorSum(int n)
{
int sum = 0;
// Loop to iterate over all the
// numbers from 1 to N
for (int i = 1; i <= n; ++i) {
// Find all divisors of
// i and add them
for (int j = 1; j * j <= i; ++j) {
if (i % j == 0) {
if (i / j == j)
sum += j;
else
sum += j + i / j;
}
}
// Subtracting 'i' so that the
// number itself is not included
sum = sum - i;
}
return sum;
}
// Driver Code
int main()
{
int n = 4;
cout << properDivisorSum(n) << endl;
n = 5;
cout << properDivisorSum(n) << endl;
return 0;
}
Java
// Java implemenation to find sum of all
// proper divisor of number up to N
class GFG {
// Utility function to find sum of
// all proper divisor of number up to N
static int properDivisorSum(int n)
{
int sum = 0;
// Loop to iterate over all the
// numbers from 1 to N
for (int i = 1; i <= n; ++i) {
// Find all divisors of
// i and add them
for (int j = 1; j * j <= i; ++j) {
if (i % j == 0) {
if (i / j == j)
sum += j;
else
sum += j + i / j;
}
}
// Subtracting 'i' so that the
// number itself is not included
sum = sum - i;
}
return sum;
}
// Driver Code
public static void main (String[] args)
{
int n = 4;
System.out.println(properDivisorSum(n));
n = 5;
System.out.println(properDivisorSum(n)) ;
}
}
// This code is contributed by Yash_R
Python3
# Python3 implemenation to find sum of all
# proper divisor of number up to N
# Utility function to find sum of
# all proper divisor of number up to N
def properDivisorSum(n):
sum = 0
# Loop to iterate over all the
# numbers from 1 to N
for i in range(n+1):
# Find all divisors of
# i and add them
for j in range(1, i + 1):
if j * j > i:
break
if (i % j == 0):
if (i // j == j):
sum += j
else:
sum += j + i // j
# Subtracting 'i' so that the
# number itself is not included
sum = sum - i
return sum
# Driver Code
if __name__ == '__main__':
n = 4
print(properDivisorSum(n))
n = 5
print(properDivisorSum(n))
# This code is contributed by mohit kumar 29
C#
// C# implemenation to find sum of all
// proper divisor of number up to N
using System;
class GFG {
// Utility function to find sum of
// all proper divisor of number up to N
static int properDivisorSum(int n)
{
int sum = 0;
// Loop to iterate over all the
// numbers from 1 to N
for (int i = 1; i <= n; ++i) {
// Find all divisors of
// i and add them
for (int j = 1; j * j <= i; ++j) {
if (i % j == 0) {
if (i / j == j)
sum += j;
else
sum += j + i / j;
}
}
// Subtracting 'i' so that the
// number itself is not included
sum = sum - i;
}
return sum;
}
// Driver Code
public static void Main (string[] args)
{
int n = 4;
Console.WriteLine(properDivisorSum(n));
n = 5;
Console.WriteLine(properDivisorSum(n)) ;
}
}
// This code is contributed by Yash_R
Javascript
C++
// C++ implementation to find sum of all
// proper divisor of numbers up to N
#include
using namespace std;
// Utility function to find sum of
// all proper divisor of number up to N
int properDivisorSum(int n)
{
int sum = 0;
// Loop to find the proper
// divisor of every number
// from 1 to N
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum - n * (n + 1) / 2;
}
// Driver Code
int main()
{
int n = 4;
cout << properDivisorSum(n) << endl;
n = 5;
cout << properDivisorSum(n) << endl;
return 0;
}
Java
// Java implementation to find sum of all
// proper divisor of numbers up to N
// Utility function to find sum of
// all proper divisor of number up to N
class GFG
{
static int properDivisorSum(int n)
{
int sum = 0;
int i;
// Loop to find the proper
// divisor of every number
// from 1 to N
for (i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum - n * (n + 1) / 2;
}
// Driver Code
public static void main(String []args)
{
int n = 4;
System.out.println(properDivisorSum(n));
n = 5;
System.out.println(properDivisorSum(n));
}
}
Python3
# Python3 implementation to find sum of all
# proper divisor of numbers up to N
# Utility function to find sum of
# all proper divisor of number up to N
def properDivisorSum(n):
sum = 0
# Loop to find the proper
# divisor of every number
# from 1 to N
for i in range(1, n + 1):
sum += (n // i) * i
return sum - n * (n + 1) // 2
# Driver Code
n = 4
print(properDivisorSum(n))
n = 5
print(properDivisorSum(n))
# This code is contributed by shubhamsingh10
C#
// C# implementation to find sum of all
// proper divisor of numbers up to N
// Utility function to find sum of
// all proper divisor of number up to N
using System;
class GFG
{
static int properDivisorSum(int n)
{
int sum = 0;
int i;
// Loop to find the proper
// divisor of every number
// from 1 to N
for (i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum - n * (n + 1) / 2;
}
// Driver Code
public static void Main(String []args)
{
int n = 4;
Console.WriteLine(properDivisorSum(n));
n = 5;
Console.WriteLine(properDivisorSum(n));
}
}
// This code is contributed by 29AjayKumar
Javascript
5
6
时间复杂度: O(N *√N)
辅助空间: O(1)
高效的方法:观察函数的模式后,可以看到“对于给定的数字N,在[1,N]范围内的每个数字’x’发生(N / x)次” 。
例如:
Let N = 6 => G(N) = F(1) + F(2) + F(3) + F(4) + F(5) + F(6)
x = 1 => 1 will occurs 6 times (in F(1), F(2), F(3), F(4), F(5) and F(6))
x = 2 => 2 will occurs 3 times (in F(2), F(4) and F(6))
x = 3 => 3 will occur 2 times (in F(3) and F(6))
x = 4 => 4 will occur 1 times (in F(4))
x = 5 => 5 will occur 1 times (in F(5))
x = 6 => 6 will occur 1 times (in F(6))
从上面的观察中,可以容易地观察到,数字x仅以其小于或等于N的倍数出现。因此,对于[1,N]中x的每个值,我们只需要查找此类倍数的计数,然后将其乘以x即可。然后将此值添加到最终总和中。
下面是上述方法的实现:
C++
// C++ implementation to find sum of all
// proper divisor of numbers up to N
#include
using namespace std;
// Utility function to find sum of
// all proper divisor of number up to N
int properDivisorSum(int n)
{
int sum = 0;
// Loop to find the proper
// divisor of every number
// from 1 to N
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum - n * (n + 1) / 2;
}
// Driver Code
int main()
{
int n = 4;
cout << properDivisorSum(n) << endl;
n = 5;
cout << properDivisorSum(n) << endl;
return 0;
}
Java
// Java implementation to find sum of all
// proper divisor of numbers up to N
// Utility function to find sum of
// all proper divisor of number up to N
class GFG
{
static int properDivisorSum(int n)
{
int sum = 0;
int i;
// Loop to find the proper
// divisor of every number
// from 1 to N
for (i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum - n * (n + 1) / 2;
}
// Driver Code
public static void main(String []args)
{
int n = 4;
System.out.println(properDivisorSum(n));
n = 5;
System.out.println(properDivisorSum(n));
}
}
Python3
# Python3 implementation to find sum of all
# proper divisor of numbers up to N
# Utility function to find sum of
# all proper divisor of number up to N
def properDivisorSum(n):
sum = 0
# Loop to find the proper
# divisor of every number
# from 1 to N
for i in range(1, n + 1):
sum += (n // i) * i
return sum - n * (n + 1) // 2
# Driver Code
n = 4
print(properDivisorSum(n))
n = 5
print(properDivisorSum(n))
# This code is contributed by shubhamsingh10
C#
// C# implementation to find sum of all
// proper divisor of numbers up to N
// Utility function to find sum of
// all proper divisor of number up to N
using System;
class GFG
{
static int properDivisorSum(int n)
{
int sum = 0;
int i;
// Loop to find the proper
// divisor of every number
// from 1 to N
for (i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum - n * (n + 1) / 2;
}
// Driver Code
public static void Main(String []args)
{
int n = 4;
Console.WriteLine(properDivisorSum(n));
n = 5;
Console.WriteLine(properDivisorSum(n));
}
}
// This code is contributed by 29AjayKumar
Java脚本
5
6
时间复杂度: O(N)
辅助空间: O(1)