Expressing a number as sum of consecutive | Set 2 (Using odd factors)
Last Updated :
10 Jun, 2022
Given a number n, find the number of ways to represent this number as a sum of 2 or more consecutive natural numbers. Examples :
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
We have already discussed one approach in below post. Count ways to express a number as sum of consecutive numbers Here a new approach is discussed. Suppose that we are talking about the sum of numbers from X to Y ie [X, X+1, …, Y-1, Y] Then the arithmetic sum is
(Y+X)(Y-X+1)/2
If this should be N, then
2N = (Y+X)(Y-X+1)
Note that one of the factors should be even and the other should be odd because Y-X+1 and Y+X should have opposite parity because Y-X and Y+X have the same parity. Since 2N is anyways even, we find the number of odd factors of N. For example, n = 15 all odd factors of 15 are 1 3 and 5 so the answer is 3.
C++
#include <bits/stdc++.h>
using namespace std;
int countOddFactors( long long n)
{
int odd_factors = 0;
for ( int i = 1; 1ll * i * i <= n; i++) {
if (n % i == 0) {
if (1ll * i * i == n) {
if (i & 1)
odd_factors++;
}
else {
if (i & 1)
odd_factors++;
int factor = n / i;
if (factor & 1)
odd_factors++;
}
}
}
return odd_factors - 1;
}
int main()
{
long long int N = 15;
cout << countOddFactors(N) << endl;
N = 10;
cout << countOddFactors(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countOddFactors( long n)
{
int odd_factors = 0 ;
for ( int i = 1 ; 1 * i * i <= n; i++)
{
if (n % i == 0 )
{
if ( 1 * i * i == n)
{
if ((i & 1 ) == 1 )
odd_factors++;
}
else {
if ((i & 1 ) == 1 )
odd_factors++;
int factor = ( int )n / i;
if ((factor & 1 ) == 1 )
odd_factors++;
}
}
}
return odd_factors - 1 ;
}
public static void main(String args[])
{
long N = 15 ;
System.out.println(countOddFactors(N));
N = 10 ;
System.out.println(countOddFactors(N));
}
}
|
Python3
def countOddFactors(n) :
odd_factors = 0
i = 1
while (( 1 * i * i) < = n) :
if (n % i = = 0 ) :
if ( 1 * i * i = = n) :
if (i & 1 ) :
odd_factors = odd_factors + 1
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
N = 15
print (countOddFactors(N))
N = 10
print (countOddFactors(N))
|
C#
using System;
class GFG
{
static int countOddFactors( long n)
{
int odd_factors = 0;
for ( int i = 1; 1 * i * i <= n; i++)
{
if (n % i == 0)
{
if (1 * i * i == n)
{
if ((i & 1) == 1)
odd_factors++;
}
else {
if ((i & 1) == 1)
odd_factors++;
int factor = ( int )n / i;
if ((factor & 1) == 1)
odd_factors++;
}
}
}
return odd_factors - 1;
}
static void Main()
{
long N = 15;
Console.WriteLine(countOddFactors(N));
N = 10;
Console.WriteLine(countOddFactors(N));
}
}
|
PHP
<?php
function countOddFactors( $n )
{
$odd_factors = 0;
for ( $i = 1; 1 * $i * $i <= $n ; $i ++)
{
if ( $n % $i == 0)
{
if (1 * $i * $i == $n )
{
if ( $i & 1)
$odd_factors ++;
}
else {
if ( $i & 1)
$odd_factors ++;
$factor = $n / $i ;
if ( $factor & 1)
$odd_factors ++;
}
}
}
return $odd_factors - 1;
}
$N = 15;
echo (countOddFactors( $N ) . ( "\n" ));
$N = 10;
echo (countOddFactors( $N ));
?>
|
Javascript
<script>
function countOddFactors(n)
{
let odd_factors = 0;
for (let i = 1; 1 * i * i <= n; i++) {
if (n % i == 0) {
if (1 * i * i == n) {
if (i & 1)
odd_factors++;
}
else {
if (i & 1)
odd_factors++;
let factor = n / i;
if (factor & 1)
odd_factors++;
}
}
}
return odd_factors - 1;
}
let N = 15;
document.write(countOddFactors(N), "</br>" );
N = 10;
document.write(countOddFactors(N), "</br>" );
</script>
|
The Time complexity for this program is O(N^0.5).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...