给定正整数n。找到的价值
其中将编号i的函数F(i)定义为“ i ”的所有除数之和。
例子 :
Input: 4
Output: 15
Explanation
F(1) = 1
F(2) = 1 + 2 = 3
F(3) = 1 + 3 = 4
F(4) = 1 + 2 + 4 = 7
ans = F(1) + F(2) + F(3) + F(4)
= 1 + 3 + 4 + 7
= 15
Input: 5
Output: 21
天真的方法是遍历每个数字(1到n),找到所有除数,并不断用该除数更新和。请参阅此内容以了解更多信息。
C++
// C++ program to find sum of all
// divisor of number up to 'n'
#include
using namespace std;
// Utility function to find sum of
// all divisor of number up to 'n'
int divisorSum(int n)
{
int sum = 0;
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;
}
}
}
return sum;
}
// Driver code
int main()
{
int n = 4;
cout << " " << divisorSum(n) << endl;
n = 5;
cout << " " << divisorSum(n);
return 0;
}
Java
// JAVA program to find sum of all
// divisor of number up to 'n'
import java.io.*;
class GFG {
// Utility function to find sum of
// all divisor of number up to 'n'
static int divisorSum(int n)
{
int sum = 0;
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;
}
}
}
return sum;
}
// Driver code
public static void main(String args[])
{
int n = 4;
System.out.println(divisorSum(n));
n = 5;
System.out.println(divisorSum(n));
}
}
/*This code is contributed by Nikita tiwari.*/
Python3
# Python3 code to find sum of all
# divisor of number up to 'n'
# Utility function to find sum of
# all divisor of number up to 'n'
def divisorSum( n ):
sum = 0
for i in range(1, n + 1):
# Find all divisors of i
# and add them
j = 1
while j * j <= i:
if i % j == 0:
if i / j == j:
sum += j
else:
sum += j + i / j
j = j + 1
return int(sum)
# Driver code
n = 4
print( divisorSum(n))
n = 5
print( divisorSum(n))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to find sum of all
// divisor of number up to 'n'
using System;
class GFG {
// Utility function to find sum of
// all divisor of number up to 'n'
static int divisorSum(int n)
{
int sum = 0;
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;
}
}
}
return sum;
}
// Driver code
public static void Main()
{
int n = 4;
Console.WriteLine(divisorSum(n));
n = 5;
Console.WriteLine(divisorSum(n));
}
}
/*This code is contributed by vt_m.*/
PHP
Javascript
C++
// C++ program to find sum of all
// divisor of number up to 'n'
#include
using namespace std;
// Utility function to find sum of
// all divisor of number up to 'n'
int divisorSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum;
}
// Driver code
int main()
{
int n = 4;
cout <<" "<< divisorSum(n)<
C
// C program to find sum of all
// divisor of number up to 'n'
#include
// Utility function to find sum of
// all divisor of number up to 'n'
int divisorSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum;
}
// Driver code
int main()
{
int n = 4;
printf("%d\n", divisorSum(n));
n = 5;
printf("%d", divisorSum(n));
return 0;
}
Java
// Java program to find sum of all
// divisor of number up to 'n'
import java.io.*;
class GFG {
// Utility function to find sum of
// all divisor of number up to 'n'
static int divisorSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum;
}
// Driver code
public static void main(String args[])
{
int n = 4;
System.out.println(divisorSum(n));
n = 5;
System.out.println(divisorSum(n));
}
}
/*This code is contributed by Nikita Tiwari.*/
Python3
# Python3 code to find sum of all
# divisor of number up to 'n'
# Utility function to find sum of
# all divisor of number up to 'n'
def divisorSum( n ):
sum = 0
for i in range(1, n + 1):
sum += int(n / i) * i
return int(sum)
# Driver code
n = 4
print( divisorSum(n))
n = 5
print( divisorSum(n))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to find sum of all
// divisor of number up to 'n'
using System;
class GFG {
// Utility function to find sum of
// all divisor of number up to 'n'
static int divisorSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum;
}
// Driver code
public static void Main()
{
int n = 4;
Console.WriteLine(divisorSum(n));
n = 5;
Console.WriteLine(divisorSum(n));
}
}
/*This code is contributed by vt_m.*/
PHP
Javascript
// Javascript program to find sum of all
// divisor of number up to 'n'
// Utility function to find sum of
// all divisor of number up to 'n'
function divisorSum(n)
{
let sum = 0;
for (let i = 1; i <= n; ++i)
sum += Math.floor(n / i) * i;
return sum;
}
// Driver code
let n = 4;
document.write(divisorSum(n) + "
");
n = 5;
document.write(divisorSum(n) + "
");
// This code is contributed by _saurabh_jaiswal.
C++
// C++ program to calculate sum of divisors
// of numbers from 1 to N in O(sqrt(N)) complexity
#include
using namespace std;
#define ll long long
#define mod 1000000007
/*
Function to calculate x^y using
Modular exponentiation
Refer to https://www.geeksforgeeks.org/
modular-exponentiation-power-in-modular-arithmetic/
*/
ll power(ll x, ll y, ll p)
{
// re x^y if p not specified
// else (x^y)%p
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return (res + p) % p;
}
// Function to find modular
// inverse of a under modulo m
// Assumption: m is prime
ll modinv(ll x)
{
return power(x, mod - 2, mod);
}
// Function to calculate sum from 1 to n
ll sum(ll n)
{
// sum 1 to n = (n*(n+1))/2
ll retval = ((((n % mod) * ((n + 1) %
mod)) % mod) * modinv(2)) % mod;
return retval;
}
ll divisorSum(ll n)
{
ll l = 1;
ll ans = 0;
while (l <= n)
{
ll k = n / l;
ll r = n / k;
k %= mod;
// For i=l to i=r, floor(n/i) will be k
ans += ((sum(r) - sum(l - 1) %
mod) * k) % mod;
// Since values can be very large
// we need to take mod at every step
ans %= mod;
l = r + 1;
}
ans = ans % mod;
// ans can be negative
// for example n = 831367 ans would be -534577982
if (ans < 0){
return ans+mod;
}else{
return ans;
}
}
/* Driver program to test above function */
int main()
{
int n = 5;
cout << "The sum of divisors of all \
numbers from 1 to " << n << " is: " \
<< divisorSum(n) << '\n';
n = 14;
cout << "The sum of divisors of all \
numbers from 1 to " << n << " is: " \
<< divisorSum(n) << '\n';
}
Java
// Java program to calculate
// sum of divisors of numbers
// from 1 to N in O(sqrt(N))
// complexity
import java.util.*;
class Main{
static int mod = 1000000007;
/*
Function to calculate x^y using
Modular exponentiation
Refer to https://www.geeksforgeeks.org/
modular-exponentiation-power-in-
modular-arithmetic/
*/
public static long power(long x,
long y,
long p)
{
// re x^y if p not specified
// else (x^y)%p
long res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return (res + p) % p;
}
// Function to find modular
// inverse of a under modulo m
// Assumption: m is prime
public static long modinv(long x)
{
return power(x, mod - 2, mod);
}
// Function to calculate sum
// from 1 to n
public static long sum(long n)
{
// sum 1 to n = (n*(n+1))/2
long retval = ((((n % mod) * ((n + 1) %
mod)) % mod) * modinv(2)) %
mod;
return retval;
}
public static long divisorSum(long n)
{
long l = 1;
long ans = 0;
while (l <= n)
{
long k = n / l;
long r = n / k;
k %= mod;
// For i=l to i=r,
// floor(n/i) will be k
ans += ((sum(r) - sum(l - 1) %
mod) * k) % mod;
// Since values can be very
// large we need to take mod
// at every step
ans %= mod;
l = r + 1;
}
ans = ans % mod;
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 5;
System.out.println("The sum of divisors of" +
" all numbers from 1 to " +
n + " is: " + divisorSum(n));
n = 14;
System.out.println("The sum of divisors of all" +
" numbers from 1 to " + n +
" is: " + divisorSum(n));
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python program to calculate
# sum of divisors of numbers
# from 1 to N in O(sqrt(N))
# complexity
mod = 1000000007;
# Function to calculate x^y using Modular exponentiation Refer to
# https:#www.geeksforgeeks.org/ modular-exponentiation-power-in-
# modular-arithmetic/
def power(x, y, p):
# re x^y if p not specified
# else (x^y)%p
res = 1;
x = x % p;
while (y > 0):
if ((y & 1) != 0):
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
return (res + p) % p;
# Function to find modular
# inverse of a under modulo m
# Assumption: m is prime
def modinv(x):
return power(x, mod - 2, mod);
# Function to calculate sum
# from 1 to n
def sum(n):
# sum 1 to n = (n*(n+1))/2
retval = ((((n % mod) * ((n + 1) % mod)) % mod) * modinv(2)) % mod;
return retval;
def divisorSum(n):
l = 1;
ans = 0;
while (l <= n):
k = n // l;
r = n // k;
k %= mod;
# For i=l to i=r,
# floor(n/i) will be k
ans += ((sum(r) - sum(l - 1) % mod) * k) % mod;
# Since values can be very
# large we need to take mod
# at every step
ans %= mod;
l = r + 1;
ans = ans % mod;
return ans;
# Driver code
if __name__ == '__main__':
n = 5;
print("The sum of divisors of all numbers from 1 to " , n , " is: " ,int( divisorSum(n)));
n = 14;
print("The sum of divisors of all numbers from 1 to ", n ," is: " , int(divisorSum(n)));
# This code contributed by aashish1995 Write
C#
// C# program to calculate
// sum of divisors of numbers
// from 1 to N in O(sqrt(N))
// complexity
using System;
class GFG{
static int mod = 1000000007;
/*
Function to calculate x^y using
Modular exponentiation
Refer to https://www.geeksforgeeks.org/
modular-exponentiation-power-in-
modular-arithmetic/
*/
static long power(long x, long y, long p)
{
// re x^y if p not specified
// else (x^y)%p
long res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return (res + p) % p;
}
// Function to find modular
// inverse of a under modulo m
// Assumption: m is prime
static long modinv(long x)
{
return power(x, mod - 2, mod);
}
// Function to calculate sum
// from 1 to n
static long sum(long n)
{
// sum 1 to n = (n*(n+1))/2
long retval = ((((n % mod) * ((n + 1) %
mod)) % mod) * modinv(2)) %
mod;
return retval;
}
static long divisorSum(long n)
{
long l = 1;
long ans = 0;
while (l <= n)
{
long k = n / l;
long r = n / k;
k %= mod;
// For i=l to i=r,
// floor(n/i) will be k
ans += ((sum(r) - sum(l - 1) %
mod) * k) % mod;
// Since values can be very
// large we need to take mod
// at every step
ans %= mod;
l = r + 1;
}
ans = ans % mod;
return ans;
}
// Driver code
static void Main()
{
int n = 5;
Console.WriteLine("The sum of divisors of" +
" all numbers from 1 to " +
n + " is: " + divisorSum(n));
n = 14;
Console.WriteLine("The sum of divisors of all" +
" numbers from 1 to " + n +
" is: " + divisorSum(n));
}
}
// This code is contributed by divyesh072019
输出 :
15
21
时间复杂度: O(n√(n)))
辅助空间: O(1)
高效的方法是观察函数并关联模式。对于给定的数字n,从1到n的每个数字都会以小于n的最高倍数表示其存在。例如,
Let n = 6,
=> F(1) + F(2) + F(3) + F(4) + F(5) + F(6)
=> 1 will occurs 6 times in F(1), F(2),
F(3), F(4), F(5) and F(6)
=> 2 will occurs 3 times in F(2), F(4) and
F(6)
=> 3 will occur 2 times in F(3) and F(6)
=> 4 will occur 1 times in F(4)
=> 5 will occur 1 times in F(5)
=> 6 will occur 1 times in F(6)
从上面的观察中,可以很容易地观察到数字i仅以小于或等于n的倍数出现。因此,我们只需要找到倍数,然后将其乘以i就可以得到最终总和。取(n / i)的底数,然后将其与i相乘即可轻松地在O(1)时间内完成。
C++
// C++ program to find sum of all
// divisor of number up to 'n'
#include
using namespace std;
// Utility function to find sum of
// all divisor of number up to 'n'
int divisorSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum;
}
// Driver code
int main()
{
int n = 4;
cout <<" "<< divisorSum(n)<
C
// C program to find sum of all
// divisor of number up to 'n'
#include
// Utility function to find sum of
// all divisor of number up to 'n'
int divisorSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum;
}
// Driver code
int main()
{
int n = 4;
printf("%d\n", divisorSum(n));
n = 5;
printf("%d", divisorSum(n));
return 0;
}
Java
// Java program to find sum of all
// divisor of number up to 'n'
import java.io.*;
class GFG {
// Utility function to find sum of
// all divisor of number up to 'n'
static int divisorSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum;
}
// Driver code
public static void main(String args[])
{
int n = 4;
System.out.println(divisorSum(n));
n = 5;
System.out.println(divisorSum(n));
}
}
/*This code is contributed by Nikita Tiwari.*/
Python3
# Python3 code to find sum of all
# divisor of number up to 'n'
# Utility function to find sum of
# all divisor of number up to 'n'
def divisorSum( n ):
sum = 0
for i in range(1, n + 1):
sum += int(n / i) * i
return int(sum)
# Driver code
n = 4
print( divisorSum(n))
n = 5
print( divisorSum(n))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to find sum of all
// divisor of number up to 'n'
using System;
class GFG {
// Utility function to find sum of
// all divisor of number up to 'n'
static int divisorSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += (n / i) * i;
return sum;
}
// Driver code
public static void Main()
{
int n = 4;
Console.WriteLine(divisorSum(n));
n = 5;
Console.WriteLine(divisorSum(n));
}
}
/*This code is contributed by vt_m.*/
的PHP
Java脚本
// Javascript program to find sum of all
// divisor of number up to 'n'
// Utility function to find sum of
// all divisor of number up to 'n'
function divisorSum(n)
{
let sum = 0;
for (let i = 1; i <= n; ++i)
sum += Math.floor(n / i) * i;
return sum;
}
// Driver code
let n = 4;
document.write(divisorSum(n) + "
");
n = 5;
document.write(divisorSum(n) + "
");
// This code is contributed by _saurabh_jaiswal.
输出 :
15
21
时间复杂度: O(n)
辅助空间: O(1)
更有效的解决方案:
我们需要计算
为了评估O(sqrt(N))中的上述表达式,我们使用了Harmonic Lemma。
考虑整数除法上的谐波序列:{N / 1,N / 2,N / 3,…..,N / N}
引理指出,以上序列不增加,并且最多存在2 * sqrt(N)个不同的元素。
考虑楼板(N / i)= k。因此,k≤N/i≤k+ 1。从中我们得到最大=地板(N / k)。因此,我们可以找到floor(N / i)恒定的i值范围。并且使用谐波引理我们知道最多为2 * sqrt(N)项,因此我们可以通过编程以O(sqrt(N))复杂度进行计算。请考虑以下示例,以更好地进行说明。
C++
// C++ program to calculate sum of divisors
// of numbers from 1 to N in O(sqrt(N)) complexity
#include
using namespace std;
#define ll long long
#define mod 1000000007
/*
Function to calculate x^y using
Modular exponentiation
Refer to https://www.geeksforgeeks.org/
modular-exponentiation-power-in-modular-arithmetic/
*/
ll power(ll x, ll y, ll p)
{
// re x^y if p not specified
// else (x^y)%p
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return (res + p) % p;
}
// Function to find modular
// inverse of a under modulo m
// Assumption: m is prime
ll modinv(ll x)
{
return power(x, mod - 2, mod);
}
// Function to calculate sum from 1 to n
ll sum(ll n)
{
// sum 1 to n = (n*(n+1))/2
ll retval = ((((n % mod) * ((n + 1) %
mod)) % mod) * modinv(2)) % mod;
return retval;
}
ll divisorSum(ll n)
{
ll l = 1;
ll ans = 0;
while (l <= n)
{
ll k = n / l;
ll r = n / k;
k %= mod;
// For i=l to i=r, floor(n/i) will be k
ans += ((sum(r) - sum(l - 1) %
mod) * k) % mod;
// Since values can be very large
// we need to take mod at every step
ans %= mod;
l = r + 1;
}
ans = ans % mod;
// ans can be negative
// for example n = 831367 ans would be -534577982
if (ans < 0){
return ans+mod;
}else{
return ans;
}
}
/* Driver program to test above function */
int main()
{
int n = 5;
cout << "The sum of divisors of all \
numbers from 1 to " << n << " is: " \
<< divisorSum(n) << '\n';
n = 14;
cout << "The sum of divisors of all \
numbers from 1 to " << n << " is: " \
<< divisorSum(n) << '\n';
}
Java
// Java program to calculate
// sum of divisors of numbers
// from 1 to N in O(sqrt(N))
// complexity
import java.util.*;
class Main{
static int mod = 1000000007;
/*
Function to calculate x^y using
Modular exponentiation
Refer to https://www.geeksforgeeks.org/
modular-exponentiation-power-in-
modular-arithmetic/
*/
public static long power(long x,
long y,
long p)
{
// re x^y if p not specified
// else (x^y)%p
long res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return (res + p) % p;
}
// Function to find modular
// inverse of a under modulo m
// Assumption: m is prime
public static long modinv(long x)
{
return power(x, mod - 2, mod);
}
// Function to calculate sum
// from 1 to n
public static long sum(long n)
{
// sum 1 to n = (n*(n+1))/2
long retval = ((((n % mod) * ((n + 1) %
mod)) % mod) * modinv(2)) %
mod;
return retval;
}
public static long divisorSum(long n)
{
long l = 1;
long ans = 0;
while (l <= n)
{
long k = n / l;
long r = n / k;
k %= mod;
// For i=l to i=r,
// floor(n/i) will be k
ans += ((sum(r) - sum(l - 1) %
mod) * k) % mod;
// Since values can be very
// large we need to take mod
// at every step
ans %= mod;
l = r + 1;
}
ans = ans % mod;
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 5;
System.out.println("The sum of divisors of" +
" all numbers from 1 to " +
n + " is: " + divisorSum(n));
n = 14;
System.out.println("The sum of divisors of all" +
" numbers from 1 to " + n +
" is: " + divisorSum(n));
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python program to calculate
# sum of divisors of numbers
# from 1 to N in O(sqrt(N))
# complexity
mod = 1000000007;
# Function to calculate x^y using Modular exponentiation Refer to
# https:#www.geeksforgeeks.org/ modular-exponentiation-power-in-
# modular-arithmetic/
def power(x, y, p):
# re x^y if p not specified
# else (x^y)%p
res = 1;
x = x % p;
while (y > 0):
if ((y & 1) != 0):
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
return (res + p) % p;
# Function to find modular
# inverse of a under modulo m
# Assumption: m is prime
def modinv(x):
return power(x, mod - 2, mod);
# Function to calculate sum
# from 1 to n
def sum(n):
# sum 1 to n = (n*(n+1))/2
retval = ((((n % mod) * ((n + 1) % mod)) % mod) * modinv(2)) % mod;
return retval;
def divisorSum(n):
l = 1;
ans = 0;
while (l <= n):
k = n // l;
r = n // k;
k %= mod;
# For i=l to i=r,
# floor(n/i) will be k
ans += ((sum(r) - sum(l - 1) % mod) * k) % mod;
# Since values can be very
# large we need to take mod
# at every step
ans %= mod;
l = r + 1;
ans = ans % mod;
return ans;
# Driver code
if __name__ == '__main__':
n = 5;
print("The sum of divisors of all numbers from 1 to " , n , " is: " ,int( divisorSum(n)));
n = 14;
print("The sum of divisors of all numbers from 1 to ", n ," is: " , int(divisorSum(n)));
# This code contributed by aashish1995 Write
C#
// C# program to calculate
// sum of divisors of numbers
// from 1 to N in O(sqrt(N))
// complexity
using System;
class GFG{
static int mod = 1000000007;
/*
Function to calculate x^y using
Modular exponentiation
Refer to https://www.geeksforgeeks.org/
modular-exponentiation-power-in-
modular-arithmetic/
*/
static long power(long x, long y, long p)
{
// re x^y if p not specified
// else (x^y)%p
long res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return (res + p) % p;
}
// Function to find modular
// inverse of a under modulo m
// Assumption: m is prime
static long modinv(long x)
{
return power(x, mod - 2, mod);
}
// Function to calculate sum
// from 1 to n
static long sum(long n)
{
// sum 1 to n = (n*(n+1))/2
long retval = ((((n % mod) * ((n + 1) %
mod)) % mod) * modinv(2)) %
mod;
return retval;
}
static long divisorSum(long n)
{
long l = 1;
long ans = 0;
while (l <= n)
{
long k = n / l;
long r = n / k;
k %= mod;
// For i=l to i=r,
// floor(n/i) will be k
ans += ((sum(r) - sum(l - 1) %
mod) * k) % mod;
// Since values can be very
// large we need to take mod
// at every step
ans %= mod;
l = r + 1;
}
ans = ans % mod;
return ans;
}
// Driver code
static void Main()
{
int n = 5;
Console.WriteLine("The sum of divisors of" +
" all numbers from 1 to " +
n + " is: " + divisorSum(n));
n = 14;
Console.WriteLine("The sum of divisors of all" +
" numbers from 1 to " + n +
" is: " + divisorSum(n));
}
}
// This code is contributed by divyesh072019
输出:
The sum of divisors of all numbers from 1 to 5 is: 21
The sum of divisors of all numbers from 1 to 14 is: 165
时间复杂度: O(sqrt(N))
辅助空间: O(1)