📜  求 [L, R] 范围内所有奇数完全平方的和

📅  最后修改于: 2022-05-13 01:56:06.823000             🧑  作者: Mango

求 [L, R] 范围内所有奇数完全平方的和

给定两个整数LR 。任务是找到在[L, R]范围内完全平方的所有奇数的总和

例子

Naive Approach :解决这个问题的基本思路是遍历 L 到 R 范围内的数字,并且对于每个奇数,检查它是否是一个完美的正方形。

时间复杂度: O(RL)
辅助空间: O(1)

高效方法:解决方案的方法基于序列的数学概念。这个想法是使用前N个奇数的平方和。

请按照以下步骤解决问题:

  1. 检查 1 和刚好大于或等于 L 的完全平方奇数之间的完全平方数。
  2. 检查[1, L)范围内的奇数完全平方
  3. 计算[1, L)范围内奇数完全平方的和(sum1 )。
  4. 检查[1, R]范围内的完美正方形的数量。
  5. 检查[1, R]范围内的奇数完全平方数。
  6. 计算[1, R]范围内奇数完全平方的和(sum2)
  7. 从 sum2 中减去sum1 得到在 [L, R] 范围内的完全平方的奇数之和。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
#include 
using namespace std;
 
// Function to find sum of all the odd
// numbers,which are perfect squares
// in range [L, R]
int findSum(int L, int R)
{
    // If L > R or both less than 0
    if (L < 0 || R < 0 || L > R)
        return -1;
 
    int l, r, n1, n2, s1, s2;
 
    // Check count of numbers
    // which are perfect squares between 
    // 1 & perfect squared odd number
    // just greater or equal to L
    l = ceil(sqrt(L));
    if (!(l & 1))
        l++;
 
    // Check count of numbers which
    // are perfect squares in range [1, R]
    r = floor(sqrt(R));
    if (!(r & 1))
        r--;
 
    // Check count of odd numbers which
    // are perfect squares in range [1, L)
    n1 = floor((float)l / 2);
 
    // Check count of odd numbers which
    // are perfect squares in range [1, R]
    n2 = ceil((float)r / 2);
 
    // Calculate sum of odd numbers which
    // are perfect squares in range [1, L)
    s1 = n1 * ((4 * n1 * n1) - 1) / 3;
 
    // Calculate sum of odd numbers which
    // are perfect squares in range [1, R]
    s2 = n2 * ((4 * n2 * n2) - 1) / 3;
 
    // Return sum of odd numbers which
    // are perfect squares in range [L, R]
    return s2 - s1;
}
 
// Driver Code
int main()
{
    int L = 1;
    int R = 9;
 
    cout << findSum(L, R);
    return 0;
}


Java
// Java implementation for the above approach
import java.util.*;
public class GFG
{
// Function to find sum of all the odd
// numbers,which are perfect squares
// in range [L, R]
static int findSum(int L, int R)
{
    // If L > R or both less than 0
    if (L < 0 || R < 0 || L > R)
        return -1;
 
    int l, r, n1, n2, s1, s2;
 
    // Check count of numbers
    // which are perfect squares between 
    // 1 & perfect squared odd number
    // just greater or equal to L
    l = (int)Math.ceil(Math.sqrt(L));
    if ((l & 1) == 0)
        l++;
 
    // Check count of numbers which
    // are perfect squares in range [1, R]
    r = (int)Math.floor(Math.sqrt(R));
    if ((r & 1) == 0)
        r--;
 
    // Check count of odd numbers which
    // are perfect squares in range [1, L)
    n1 = (int)Math.floor((float)l / 2);
 
    // Check count of odd numbers which
    // are perfect squares in range [1, R]
    n2 = (int)Math.ceil((float)r / 2);
 
    // Calculate sum of odd numbers which
    // are perfect squares in range [1, L)
    s1 = n1 * ((4 * n1 * n1) - 1) / 3;
 
    // Calculate sum of odd numbers which
    // are perfect squares in range [1, R]
    s2 = n2 * ((4 * n2 * n2) - 1) / 3;
 
    // Return sum of odd numbers which
    // are perfect squares in range [L, R]
    return s2 - s1;
}
 
// Driver Code
public static void main(String args[])
{
    int L = 1;
    int R = 9;
 
    System.out.println(findSum(L, R));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python3 implementation for the above approach
import math
 
# Function to find sum of all the odd
# numbers,which are perfect squares
# in range [L, R]
def findSum(L, R):
     
    # If L > R or both less than 0
    if (L < 0 or R < 0 or L > R):
        return -1
         
    # Check count of numbers which are
    # perfect squares between 1 & perfect
    # squared odd number just greater or
    # equal to L
    l = math.ceil(math.sqrt(L))
    if (not (l & 1)):
        l += 1
 
    # Check count of numbers which
    # are perfect squares in range [1, R]
    r = math.floor(math.sqrt(R))
    if (not (r & 1)):
        r -= 1
 
    # Check count of odd numbers which
    # are perfect squares in range [1, L)
    n1 = math.floor(l / 2)
 
    # Check count of odd numbers which
    # are perfect squares in range [1, R]
    n2 = math.ceil(r / 2)
 
    # Calculate sum of odd numbers which
    # are perfect squares in range [1, L)
    s1 = int(n1 * ((4 * n1 * n1) - 1) / 3)
 
    # Calculate sum of odd numbers which
    # are perfect squares in range [1, R]
    s2 = int(n2 * ((4 * n2 * n2) - 1) / 3)
 
    # Return sum of odd numbers which
    # are perfect squares in range [L, R]
    return s2 - s1
 
# Driver Code
if __name__ == "__main__":
 
    L = 1
    R = 9
 
    print(findSum(L, R))
 
# This code is contributed by rakeshsahni


C#
// C# implementation for the above approach
using System;
class GFG
{
   
// Function to find sum of all the odd
// numbers,which are perfect squares
// in range [L, R]
static int findSum(int L, int R)
{
   
    // If L > R or both less than 0
    if (L < 0 || R < 0 || L > R)
        return -1;
 
    int l, r, n1, n2, s1, s2;
 
    // Check count of numbers
    // which are perfect squares between 
    // 1 & perfect squared odd number
    // just greater or equal to L
    l = (int)Math.Ceiling(Math.Sqrt(L));
    if ((l & 1) == 0)
        l++;
 
    // Check count of numbers which
    // are perfect squares in range [1, R]
    r = (int)Math.Floor(Math.Sqrt(R));
    if ((r & 1) == 0)
        r--;
 
    // Check count of odd numbers which
    // are perfect squares in range [1, L)
    n1 = (int)Math.Floor((float)l / 2);
 
    // Check count of odd numbers which
    // are perfect squares in range [1, R]
    n2 = (int)Math.Ceiling((float)r / 2);
 
    // Calculate sum of odd numbers which
    // are perfect squares in range [1, L)
    s1 = n1 * ((4 * n1 * n1) - 1) / 3;
 
    // Calculate sum of odd numbers which
    // are perfect squares in range [1, R]
    s2 = n2 * ((4 * n2 * n2) - 1) / 3;
 
    // Return sum of odd numbers which
    // are perfect squares in range [L, R]
    return s2 - s1;
}
 
// Driver Code
public static void Main()
{
    int L = 1;
    int R = 9;
 
    Console.Write(findSum(L, R));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
10

时间复杂度:O(1)
辅助空间:O(1)