Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | C ++ program to count number of ways to express N as sum of consecutive numbers . ; returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; Driver Code ; N as sum of consecutive numbers
#include <bits/stdc++.h> NEW_LINE 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 ; }