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

📅  最后修改于: 2021-09-06 11:30:24             🧑  作者: Mango

给定两个表示范围[L, R] 的数字L , R ,任务是找到位于范围 [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) 中得到回答。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live