If you want a function to just return the sum to all integers in range from low
to high
then you can do it even faster with some simple maths
you can consider an arithmetic sequence starting from low
and going to high
with a common difference of 1 ,and have (high - low + 1
) elements in it.
then the sum will straight up be :-
sum = ( (high * ( high + 1 )) - ((low * (low - 1)) ) / 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…