📌  相关文章
📜  给定范围内的数字的最大奇数和

📅  最后修改于: 2021-05-07 10:20:38             🧑  作者: Mango

给定整数间隔[A,B]。对于此间隔中的每个数字,计算其最大奇数除数。输出这些除数的总和。

例子:

Input : A = 1, B = 3
Output : 5
1 + 1 + 3 = 5

Input : A = 3, B = 9
Output : 29
3 + 1 + 5 + 3 + 7 + 1 + 9 = 29

天真的方法:
一种简单的方法是遍历范围内的所有数字以找到其最大的奇数除数,但是该算法的时间复杂度为O(n)。

高效的方法:
如果我们可以在范围[1,B]中找到答案,并从[1,A -1]中减去它,我们就需要在范围[A,B]中找到答案,那么我们将得到所需的答案。
在这里我们可以看到–

  • 奇数X的答案是X本身。
  • 偶数X的答案等于X / 2的答案。这是正确的,因为X和X / 2具有相同的奇数除数(如果X = 4,则4和2都具有1作为最大奇数除数)。

如果要在范围[1,N]中找到答案,那么首先我们需要确定所有奇数的总和
(1,3,5,…)使用一个简单的公式:前K个奇数之和等于K 2 。然后,我们需要为偶数(2、4、6,…)添加答案。但是这些实际上等于1,2,3,…,floor(N / 2)的答案,因此我们可以递归调用floor(N / 2)的函数。

该算法的复杂度为O(log(N))。

以下是上述想法的实现:

C++
// C++ program to find sum of greatest
// odd divisor of numbers in given range
#include 
using namespace std;
  
// Function to return sum of
// first n odd numbers
int square(int n) { return n * n; }
  
// Recursive function to return sum of greatest
// odd divisor of numbers in range [1, n]
int sum(int n)
{
    if (n == 0)
        return 0;
    if (n % 2 == 1) {  // Odd n
        return square((n + 1) / 2) + sum(n / 2);       
    }
    else { // Even n
        return square(n / 2) + sum(n / 2);
    }
}
  
// Function to return sum of greatest
// odd divisor of numbers in range [a, b]
int oddDivSum(int a, int b)
{
    return sum(b) - sum(a - 1);
}
  
// Driver code
int main()
{
    int a = 3, b = 9;
    cout << oddDivSum(a, b) << endl;
    return 0;
}


Java
// Java program to find sum of greatest
// odd divisor of numbers in given range
  
// Function to return sum of
// first n odd numbers
class gfg 
{
static int square(int n)
{
    return n * n;
}
  
// Recursive function to return sum of greatest
// odd divisor of numbers in range [1, n]
static int sum(int n)
{
    if (n == 0)
        return 0;
    if (n % 2 == 1)
    {
        // Odd n
        return square((n + 1) / 2) + sum(n / 2);     
    }
    else
    { 
        // Even n
        return square(n / 2) + sum(n / 2);
    }
}
  
// Function to return sum of greatest
// odd divisor of numbers in range [a, b]
static int oddDivSum(int a, int b)
{
    return sum(b) - sum(a - 1);
}
  
// Driver code
public static void main(String[] args)
{
    int a = 3, b = 9;
    System.out.println(oddDivSum(a, b));
}
}
// This code is contributed by mits


Python3
# Python3 program to find sum of greatest
# odd divisor of numbers in given range
  
# Function to return sum of first
# n odd numbers
def square(n):
    return n * n; 
  
# Recursive function to return sum
# of greatest odd divisor of numbers 
# in range [1, n]
def sum(n):
  
    if (n == 0):
        return 0;
    if (n % 2 == 1):
          
        # Odd n
        return (square(int((n + 1) / 2)) + 
                   sum(int(n / 2))); 
    else:
          
        # Even n
        return (square(int(n / 2)) +
                   sum(int(n / 2)));
  
# Function to return sum of greatest
# odd divisor of numbers in range [a, b]
def oddDivSum(a, b):
  
    return sum(b) - sum(a - 1);
  
# Driver code
a, b = 3, 9;
print(oddDivSum(a, b));
  
# This code is contributed by mits


C#
// C# program to find sum of greatest
// odd divisor of numbers in given range
using System;
  
// Function to return sum of
// first n odd numbers
class gfg 
{
 public int square(int n)
 {
     return n * n;
 }
  
// Recursive function to return sum of greatest
// odd divisor of numbers in range [1, n]
 public int sum(int n)
 {
    if (n == 0)
        return 0;
    if (n % 2 == 1)
    {
        // Odd n
        return square((n + 1) / 2) + sum(n / 2);     
    }
    else 
    { 
        // Even n
        return square(n / 2) + sum(n / 2);
    }
}
  
// Function to return sum of greatest
// odd divisor of numbers in range [a, b]
 public int oddDivSum(int a, int b)
 {
    return sum(b) - sum(a - 1);
 }
}
  
// Driver code
class geek
{
 public static int Main()
 {
     gfg g = new gfg();
    int a = 3, b = 9;
    Console.WriteLine(g.oddDivSum(a, b));
    return 0;
 }
}


PHP


输出:
29

时间复杂度: O(log(N))