📜  求有礼貌的数字

📅  最后修改于: 2021-04-29 08:04:45             🧑  作者: Mango

给定整数n。找出数字n的礼貌。数字的礼貌定义为表示连续整数之和的方式数量。

例子:

Input: n = 15
Output: 3
Explanation:
There are only three ways to express
15 as sum of consecutive integers i.e.,
15 = 1 + 2 + 3 + 4 + 5
15 = 4 + 5 + 6
15 = 7 + 8
Hence answer is 3

Input: n = 9;
Output:  2
There are two ways of representation:
9 = 2 + 3 + 4
9 = 4 + 5
我们强烈建议您单击此处并进行实践,然后再继续解决方案。

天真的方法:

在另一个内部运行一个循环,找到每个连续的整数之和,直到n。该方法的时间复杂度将是O(n 2 ),对于大的n值将是不够的。

高效方法:

使用分解。我们将数n分解,然后计算奇数个数。奇数个因素的总数(除1之外)等于数量的礼貌程度。请参阅此以证明这一事实。通常,如果一个数字可以表示为a p * b q * c r …,其中a,b,c,…是n的素数。如果a = 2(偶数),则将其丢弃并计算可写为[(q + 1)*(r + 1)*…] – 1的奇数总数(此处减去1,因为表示中的单项为不允许)。
以上公式如何运作?事实是,如果将一个数表示为a p * b q * c r …,其中a,b,c,…是n的素数,则除数为(p + 1)*(q + 1)* (r + 1)……
为简化起见,设一个因子,数字表示为p 。除数是1,a,a 2 ,…。 p除数的数量为p + 1。现在让我们来处理一个稍微复杂的情况a p b p 。除数为:
1,a,a 2 ,…。 p
b,ba,ba 2 ,…。 p
b 2 ,b 2 a,b 2 a 2 …。 b 2 a p
………………。
………………。
b q ,b q a,b q a 2 ,…。 B&Qp
上述项的计数为(p + 1)*(q + 1)。同样,我们可以证明更多的主要因素。
插图:对于n = 90,素因数分解如下:
=> 90 = 2 * 3 2 * 5 1 。奇数素数3、5的幂分别为2和1。将上面的公式应用为:(2 +1)*(1 +1)-1 =5。因此,答案是5。我们可以对它进行交叉检查。所有奇数因子均为3、5、9、15和45。
以下是上述步骤的程序:

C++
// C+ program to find politeness of number
#include 
using namespace std;
 
// A function to count all odd prime factors
// of a given number n
int countOddPrimeFactors(int n)
{
    int result = 1;
 
    // Eliminate all even prime
    // factor of number of n
    while (n % 2 == 0)
        n /= 2;
 
    // n must be odd at this point,
    // so iterate for only
    // odd numbers till sqrt(n)
    for (int i = 3; i * i <= n; i += 2) {
        int divCount = 0;
 
        // if i divides n, then start counting of
        // Odd divisors
        while (n % i == 0) {
            n /= i;
            ++divCount;
        }
 
        result *= divCount + 1;
    }
 
    // If n odd prime still remains then count it
    if (n > 2)
        result *= 2;
 
    return result;
}
 
int politness(int n)
{
    return countOddPrimeFactors(n) - 1;
}
 
// Driver program to test above function
int main()
{
    int n = 90;
    cout << "Politness of " << n << " = "
         << politness(n) << "\n";
 
    n = 15;
    cout << "Politness of " << n << " = "
         << politness(n) << "\n";
    return 0;
}


Java
// Java program to find politeness of a number
 
public class Politeness {
    // A function to count all odd prime factors
    // of a given number n
    static int countOddPrimeFactors(int n)
    {
        int result = 1;
 
        // Eliminate all even prime
        // factor of number of n
        while (n % 2 == 0)
            n /= 2;
 
        // n must be odd at this point, so iterate
        // for only odd numbers till sqrt(n)
        for (int i = 3; i * i <= n; i += 2) {
            int divCount = 0;
 
            // if i divides n, then start counting of
            // Odd divisors
            while (n % i == 0) {
                n /= i;
                ++divCount;
            }
 
            result *= divCount + 1;
        }
        // If n odd prime still remains then count it
        if (n > 2)
            result *= 2;
 
        return result;
    }
 
    static int politness(int n)
    {
        return countOddPrimeFactors(n) - 1;
    }
 
    public static void main(String[] args)
    {
        int n = 90;
        System.out.println("Politness of " + n + " = "
                           + politness(n));
 
        n = 15;
        System.out.println("Politness of " + n + " = "
                           + politness(n));
    }
}
 
// This code is contributed by Saket Kumar


Python
# Python program to find politeness of number
 
# A function to count all odd prime factors
# of a given number n
def countOddPrimeFactors(n) :
    result = 1;
  
    # Eliminate all even prime factor of
    # number of n
    while (n % 2 == 0) :
        n /= 2
  
    # n must be odd at this point, so iterate
    # for only odd numbers till sqrt(n)
    i = 3
    while i * i <= n :
        divCount = 0
  
        # if i divides n, then start counting
        # of Odd divisors
        while (n % i == 0) :
            n /= i
            divCount = divCount + 1
        
        result = result * divCount + 1
        i = i + 2
  
    # If n odd prime still remains then count it
    if (n > 2) :
        result = result * 2
         
    return result
     
  
def politness( n) :
    return countOddPrimeFactors(n) - 1;
 
# Driver program to test above function
n = 90
print "Politness of ", n, " = ", politness(n)
n = 15
print "Politness of ", n, " = ", politness(n)
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find politeness of a number.
using System;
 
public class GFG {
     
    // A function to count all odd prime
    // factors of a given number n
    static int countOddPrimeFactors(int n)
    {
         
        int result = 1;
 
        // Eliminate all even prime factor
        // of number of n
        while (n % 2 == 0)
            n /= 2;
 
        // n must be odd at this point, so
        // iterate for only odd numbers
        // till sqrt(n)
        for (int i = 3; i * i <= n; i += 2)
        {
            int divCount = 0;
 
            // if i divides n, then start
            // counting of Odd divisors
            while (n % i == 0) {
                n /= i;
                ++divCount;
            }
 
            result *= divCount + 1;
        }
         
        // If n odd prime still remains
        // then count it
        if (n > 2)
            result *= 2;
 
        return result;
    }
 
    static int politness(int n)
    {
        return countOddPrimeFactors(n) - 1;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 90;
        Console.WriteLine("Politness of "
               + n + " = " + politness(n));
 
        n = 15;
        Console.WriteLine("Politness of "
               + n + " = " + politness(n));
    }
}
 
// This code is contributed by nitin mittal.


PHP
 2)
        $result *= 2;
 
    return $result;
}
 
function politness($n)
{
    return countOddPrimeFactors($n) - 1;
}
 
    // Driver Code
    $n = 90;
    echo "Politness of " , $n , " = "
               , politness($n), "\n";
 
    $n = 15;
    echo "Politness of " , $n , " = "
               , politness($n) ,"\n";
 
// This code is contributed by nitin mittal.
?>


Javascript


C++
// CPP program for the above approach
#include 
#include 
using namespace std;
 
// Function to find politeness
int politness(int n)
{
    int count = 0;
   
    // sqrt(2*n) as max length
    // will be when the sum starts
    // from 1
    // which follows the equation n^2 - n - (2*sum) = 0
    for (int i = 2; i <= sqrt(2 * n); i++) {
        int a;
        if ((2 * n) % i != 0)
            continue;
        a = 2 * n;
        a /= i;
        a -= (i - 1);
        if (a % 2 != 0)
            continue;
        a /= 2;
        if (a > 0) {
            count++;
        }
    }
    return count;
}
 
// Driver program to test above function
int main()
{
    int n = 90;
    cout << "Politness of " << n << " = " << politness(n)
         << "\n";
 
    n = 15;
    cout << "Politness of " << n << " = " << politness(n)
         << "\n";
    return 0;
}
 
// This code is contributed by Prajjwal Chittori


Java
// Java program for the above approach
import java.lang.Math;
public class Main {
   
  // Function to find politeness
  static int politness(int n)
  {
    int count = 0;
 
    // sqrt(2*n) as max length
    // will be when the sum
    // starts from 1
    // which follows the
    // equation n^2 - n - (2*sum) = 0
    for (int i = 2; i <= Math.sqrt(2 * n); i++) {
      int a;
      if ((2 * n) % i != 0)
        continue;
      a = 2 * n;
      a /= i;
      a -= (i - 1);
      if (a % 2 != 0)
        continue;
      a /= 2;
      if (a > 0) {
        count++;
      }
    }
    return count;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int n = 90;
    System.out.println("Politness of " + n + " = "
                       + politness(n));
 
    n = 15;
    System.out.println("Politness of " + n + " = "
                       + politness(n));
  }
}
 
// This code is contributed by Prajjwal Chittori


Python
# python program for the above approach
import math
 
# Function to find politeness
def politness(n):
    count = 0
     
    # sqrt(2*n) as max length will be
    # when the sum starts from 1
    # which follows the equation
    # n^2 - n - (2*sum) = 0
    for i in range(2, int(math.sqrt(2 * n)) + 1):
        if ((2 * n) % i != 0):
            continue
        a = 2 * n
        a = a / i
        a = a - (i - 1)
        if (a % 2 != 0):
            continue
        a /= 2
        if (a > 0):
            count = count + 1
    return count
 
 
# Driver program to test above function
n = 90
print "Politness of ", n, " = ", politness(n)
n = 15
print "Politness of ", n, " = ", politness(n)
 
# This code is contributed by Prajjwal Chittori


Output:
Politness of 90 = 5
Politness of 15 = 3

时间复杂度: O(sqrt(n))
辅助空间: O(1)
参考:维基百科

另一种有效的方法:

计算是否可以为给定的长度域[2,sqrt(2 * n)]生成AP。计算直到sqrt(2 * n)的长度的原因是-

AP 1、2、3…的最大长度

Length for this AP is -

n= ( len * (len+1) ) / 2

len2 + len - (2*n) =0

so len≈sqrt(2*n) 

因此,我们可以检查从1到sqrt(2 * n)的每个len,如果可以用该len生成AP。获得AP第一任期的公式是–

n= ( len/2) * ( (2*A1) + len-1 )

下面是上述方法的实现:

C++

// CPP program for the above approach
#include 
#include 
using namespace std;
 
// Function to find politeness
int politness(int n)
{
    int count = 0;
   
    // sqrt(2*n) as max length
    // will be when the sum starts
    // from 1
    // which follows the equation n^2 - n - (2*sum) = 0
    for (int i = 2; i <= sqrt(2 * n); i++) {
        int a;
        if ((2 * n) % i != 0)
            continue;
        a = 2 * n;
        a /= i;
        a -= (i - 1);
        if (a % 2 != 0)
            continue;
        a /= 2;
        if (a > 0) {
            count++;
        }
    }
    return count;
}
 
// Driver program to test above function
int main()
{
    int n = 90;
    cout << "Politness of " << n << " = " << politness(n)
         << "\n";
 
    n = 15;
    cout << "Politness of " << n << " = " << politness(n)
         << "\n";
    return 0;
}
 
// This code is contributed by Prajjwal Chittori

Java

// Java program for the above approach
import java.lang.Math;
public class Main {
   
  // Function to find politeness
  static int politness(int n)
  {
    int count = 0;
 
    // sqrt(2*n) as max length
    // will be when the sum
    // starts from 1
    // which follows the
    // equation n^2 - n - (2*sum) = 0
    for (int i = 2; i <= Math.sqrt(2 * n); i++) {
      int a;
      if ((2 * n) % i != 0)
        continue;
      a = 2 * n;
      a /= i;
      a -= (i - 1);
      if (a % 2 != 0)
        continue;
      a /= 2;
      if (a > 0) {
        count++;
      }
    }
    return count;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int n = 90;
    System.out.println("Politness of " + n + " = "
                       + politness(n));
 
    n = 15;
    System.out.println("Politness of " + n + " = "
                       + politness(n));
  }
}
 
// This code is contributed by Prajjwal Chittori

Python

# python program for the above approach
import math
 
# Function to find politeness
def politness(n):
    count = 0
     
    # sqrt(2*n) as max length will be
    # when the sum starts from 1
    # which follows the equation
    # n^2 - n - (2*sum) = 0
    for i in range(2, int(math.sqrt(2 * n)) + 1):
        if ((2 * n) % i != 0):
            continue
        a = 2 * n
        a = a / i
        a = a - (i - 1)
        if (a % 2 != 0):
            continue
        a /= 2
        if (a > 0):
            count = count + 1
    return count
 
 
# Driver program to test above function
n = 90
print "Politness of ", n, " = ", politness(n)
n = 15
print "Politness of ", n, " = ", politness(n)
 
# This code is contributed by Prajjwal Chittori
输出
Politness of 90 = 5
Politness of 15 = 3

时间复杂度:O(sqrt(2 * n))≈O(sqrt(n))辅助空间: O(1)