Sum of consecutive numbers: For a given positive integer n, please find out how many ways there are to express n as the sum of a number of consecutive positive integers, including at least two positive integers.

Title description

For a given positive integer n, please find out how many ways there are to express n as the sum of several consecutive positive integers, including at least two positive integers. For example, when n=15, there are 3 ways: (1+2+3+4+5), (4+5+6), (7+8).

Input data

The first line of input data is a positive integer T, which represents the number of groups of test data. In the subsequent T rows, each row includes a set of test data, which is an integer n (1≤T≤1000, n≤10^9).

Output Data

For each group of input data, output a line of result "Case #id: M", which means that the result of the id group data is M, and id starts from 1.

Sample input

2
3
15

Sample output

Case #1: 1
Case #2: 3

Experience:
1. Find the corresponding relationship between a1 and n, and use n to narrow the loop range
. 2. Deduct a1 and bring it into verification.
3. The summation formula:
Sn = n(A1+An)/2
Sn = n(A1+A1+n-1)/2
through the smallest A1 (ie A1 = 1) to get the largest n

import math

T = int(input())
for n in range(T) :
    inputStr = input()    
    if inputStr != "":        
    	M = 0        
    	N = int(inputStr)        
    	maxN = int(math.sqrt(2*N+1/4)-1/2)        
    	for n1 in range(2, maxN+1):            
    	    a1 = int((2*N-n1*(n1-1))/(2*n1))            
    	    if a1 == 0:                
    	        continue            
    	    elif n1*(2*a1+n1-1)/2 == N:                                
    	        M += 1        
    	print("Case #%d: %d" % (n + 1 , M))
    else:
        break

Guess you like

Origin blog.csdn.net/tianxiefenxiang/article/details/107334071