Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; N as sum of consecutive numbers
def countOddFactors ( n ) : NEW_LINE INDENT odd_factors = 0 NEW_LINE i = 1 NEW_LINE while ( ( 1 * i * i ) <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( 1 * i * i == n ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT odd_factors = odd_factors + 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( ( i & 1 ) ) : NEW_LINE INDENT odd_factors = odd_factors + 1 NEW_LINE DEDENT factor = int ( n / i ) NEW_LINE if ( factor & 1 ) : NEW_LINE INDENT odd_factors = odd_factors + 1 NEW_LINE DEDENT DEDENT DEDENT i = i + 1 NEW_LINE DEDENT return odd_factors - 1 NEW_LINE DEDENT N = 15 NEW_LINE print ( countOddFactors ( N ) ) NEW_LINE N = 10 NEW_LINE print ( countOddFactors ( N ) ) NEW_LINE