给定数字n,请找到将这个数字表示为2个或更多个连续自然数之和的方式的数量。
例子 :
Input : n = 15
Output : 3
15 can be represented as:
1 + 2 + 3 + 4 + 5
4 + 5 + 6
7 + 8
Input :10
Output :2
10 can only be represented as:
1 + 2 + 3 + 4
我们已经在下面的文章中讨论了一种方法。
计算将数字表示为连续数字之和的方法
这里讨论一种新方法。假设我们正在谈论从X到Y的数字总和,即[X,X + 1,…,Y-1,Y]
那么算术和是
(Y+X)(Y-X+1)/2
如果应该为N,则
2N = (Y+X)(Y-X+1)
请注意,一个因素应该是偶数,另一个因素应该是奇数,因为Y-X + 1和Y + X应该具有相反的奇偶性,因为YX和Y + X具有相同的奇偶性。由于2N仍然是偶数,因此我们找到N的奇数个数。
例如,n = 15,所有15的奇数因子均为1 3和5,因此答案为3。
C++
// C++ program to count number of ways to express
// N as sum of consecutive numbers.
#include
using namespace std;
// returns the number of odd factors
int countOddFactors(long long n)
{
int odd_factors = 0;
for (int i = 1; 1ll * i * i <= n; i++) {
if (n % i == 0) {
// If i is an odd factor and
// n is a perfect square
if (1ll * i * i == n) {
if (i & 1)
odd_factors++;
}
// If n is not perfect square
else {
if (i & 1)
odd_factors++;
int factor = n / i;
if (factor & 1)
odd_factors++;
}
}
}
return odd_factors - 1;
}
// Driver Code
int main()
{
// N as sum of consecutive numbers
long long int N = 15;
cout << countOddFactors(N) << endl;
N = 10;
cout << countOddFactors(N) << endl;
return 0;
}
Java
// Java program to count number
// of ways to express N as sum
// of consecutive numbers.
import java.io.*;
class GFG
{
// returns the number
// of odd factors
static int countOddFactors(long n)
{
int odd_factors = 0;
for (int i = 1; 1 * i * i <= n; i++)
{
if (n % i == 0)
{
// If i is an odd factor and
// n is a perfect square
if (1 * i * i == n)
{
if ((i & 1) == 1)
odd_factors++;
}
// If n is not
// perfect square
else {
if ((i & 1) == 1)
odd_factors++;
int factor = (int)n / i;
if ((factor & 1) == 1)
odd_factors++;
}
}
}
return odd_factors - 1;
}
// Driver Code
public static void main(String args[])
{
// N as sum of consecutive numbers
long N = 15;
System.out.println(countOddFactors(N));
N = 10;
System.out.println(countOddFactors(N));
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
Python3
# Python3 program to count number
# of ways to express N as sum
# of consecutive numbers.
# returns the number
# of odd factors
def countOddFactors(n) :
odd_factors = 0
i = 1
while((1 * i * i) <= n) :
if (n % i == 0) :
# If i is an odd factor and
# n is a perfect square
if (1 * i * i == n) :
if (i & 1) :
odd_factors = odd_factors + 1
# If n is not perfect square
else :
if ((i & 1)) :
odd_factors = odd_factors + 1
factor = int(n / i)
if (factor & 1) :
odd_factors = odd_factors + 1
i = i + 1
return odd_factors - 1
# Driver Code
# N as sum of consecutive numbers
N = 15
print (countOddFactors(N))
N = 10
print (countOddFactors(N))
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# program to count number of
// ways to express N as sum of
// consecutive numbers.
using System;
class GFG
{
// returns the number
// of odd factors
static int countOddFactors(long n)
{
int odd_factors = 0;
for (int i = 1; 1 * i * i <= n; i++)
{
if (n % i == 0)
{
// If i is an odd factor and
// n is a perfect square
if (1 * i * i == n)
{
if ((i & 1) == 1)
odd_factors++;
}
// If n is not
// perfect square
else {
if ((i & 1) == 1)
odd_factors++;
int factor = (int)n / i;
if ((factor & 1) == 1)
odd_factors++;
}
}
}
return odd_factors - 1;
}
// Driver Code
static void Main()
{
// N as sum of consecutive numbers
long N = 15;
Console.WriteLine(countOddFactors(N));
N = 10;
Console.WriteLine(countOddFactors(N));
}
}
PHP
输出 :
3
1
该程序的时间复杂度为O(N ^ 0.5)。