📌  相关文章
📜  位于[L,R]范围内的所有完善数的总和

📅  最后修改于: 2021-05-07 05:34:50             🧑  作者: Mango

给定两个表示范围[L,R]的数字LR ,任务是找到位于[L,R]范围内的所有理想数字的总和。
例子:

幼稚的方法:针对此问题的幼稚的方法是通过迭代[L,R]范围内的每个数字来检查数字是否为理想数字。如果范围内有N个数字,则此方法的时间复杂度为O(N * sqrt(K)) ,其中K是[L,R]范围内的最大数(R)。
高效的方法:这个想法是使用前缀求和数组的概念来执行预计算并将所有数字的总和存储在数组中。

  • 初始化数组,直到最大大小,以使数组的每个索引’i’代表[0,i]中所有理想数的和。
  • 遍历数组,对于每个索引,检查它是否是一个完美的数字。
  • 如果它是一个完美的数字,则将存储在前一个索引(i – 1)和当前索引’i’上的总和相加。
  • 如果不是理想数字,则将存储在前一个索引(i – 1)上的总和与值0相加。
  • 最后,对于每个查询[L,R],返回值:
sum = arr[R] - arr[L - 1]

下面是上述方法的实现:

C++
// C++ implementation to find the
// sum of all perfect numbers
// lying in the range [L, R]
 
#include 
#define ll int
using namespace std;
 
// Array to store the sum
long long pref[100010];
 
// Function to check if a number is
// a perfect number or not
int isPerfect(int n)
{
    int sum = 1;
 
    // Iterating till the square root
    // of the number and checking if
    // the sum of divisors is equal
    // to the number or not
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            if (i * i != n)
                sum = sum + i + n / i;
            else
                sum = sum + i;
        }
    }
 
    // If it is a perfect number, then
    // return the number
    if (sum == n && n != 1)
        return n;
 
    // Else, return 0
    return 0;
}
 
// Function to precompute the sum
// of perfect squares and store
// then in an array
void precomputation()
{
    for (int i = 1; i <= 100000; ++i) {
        pref[i] = pref[i - 1] + isPerfect(i);
    }
}
 
int main()
{
 
    int L = 6, R = 28;
 
    precomputation();
 
    cout << pref[R] - pref[L - 1];
 
    return 0;
}


Java
// Java implementation to find the
// sum of all perfect numbers
// lying in the range [L, R]
class GFG {
     
    // Array to store the sum
    static int pref [] = new int[10000];
     
    // Function to check if a number is
    // a perfect number or not
    static int isPerfect(int n)
    {
        int sum = 1;
     
        // Iterating till the square root
        // of the number and checking if
        // the sum of divisors is equal
        // to the number or not
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                if (i * i != n)
                    sum = sum + i + n / i;
                else
                    sum = sum + i;
            }
        }
     
        // If it is a perfect number, then
        // return the number
        if (sum == n && n != 1)
            return n;
     
        // Else, return 0
        return 0;
    }
     
    // Function to precompute the sum
    // of perfect squares and store
    // then in an array
    static void precomputation()
    {
        for (int i = 1; i < 10000; ++i) {
            pref[i] = pref[i - 1] + isPerfect(i);
        }
    }
     
    public static void main (String[] args)
    {
     
        int L = 6, R = 28;
     
        precomputation();
     
        System.out.println(pref[R] - pref[L - 1]);
     
    }
 
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation to find the
# sum of all perfect numbers
# lying in the range [L, R]
 
from math import sqrt
 
# Array to store the sum
pref = [0]*10000;
 
# Function to check if a number is
# a perfect number or not
def isPerfect(n)  :
 
    sum = 1;
 
    # Iterating till the square root
    # of the number and checking if
    # the sum of divisors is equal
    # to the number or not
    for i in range(2, int(sqrt(n)) + 1) :
        if (n % i == 0) :
            if (i * i != n) :
                sum = sum + i + n // i;
            else :
                sum = sum + i;
                 
    # If it is a perfect number, then
    # return the number
    if (sum == n and n != 1) :
        return n;
 
    # Else, return 0
    return 0;
 
# Function to precompute the sum
# of perfect squares and store
# then in an array
def precomputation() :
 
    for i in range(1, 10000) :
        pref[i] = pref[i - 1] + isPerfect(i);
 
if __name__ == "__main__" :
 
 
    L = 6; R = 28;
 
    precomputation();
 
    print(pref[R] - pref[L - 1]);
 
# This code is contributed by AnkitRai01


C#
// C# implementation to find the
// sum of all perfect numbers
// lying in the range [L, R]
using System;
 
public class GFG {
      
    // Array to store the sum
    static int []pref = new int[10000];
      
    // Function to check if a number is
    // a perfect number or not
    static int isPerfect(int n)
    {
        int sum = 1;
      
        // Iterating till the square root
        // of the number and checking if
        // the sum of divisors is equal
        // to the number or not
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                if (i * i != n)
                    sum = sum + i + n / i;
                else
                    sum = sum + i;
            }
        }
      
        // If it is a perfect number, then
        // return the number
        if (sum == n && n != 1)
            return n;
      
        // Else, return 0
        return 0;
    }
      
    // Function to precompute the sum
    // of perfect squares and store
    // then in an array
    static void precomputation()
    {
        for (int i = 1; i < 10000; ++i) {
            pref[i] = pref[i - 1] + isPerfect(i);
        }
    }
      
    public static void Main(String[] args)
    {
      
        int L = 6, R = 28;
      
        precomputation();
      
        Console.WriteLine(pref[R] - pref[L - 1]);
      
    }
  
}
// This code contributed by Rajput-Ji


Javascript


输出:
34

时间复杂度:

  • 预计算所需的时间为O(K * sqrt(K)) ,其中K是执行预计算所需的数字
  • 预计算后,每个查询都在O(1)中回答。