1+2+3+....+n

 

Essentially: arithmetic sequence summation formula

sn = n * (a1 + an) / 2; (sn = n * (1 + n In this problem) / 2)
Since multiplication is not used, to expand the deformed sn = (n + n * n) / 2 -----> sn = (n + n ^ 2) / 2;
In addition to 2 can be realized right one
Multiplication can be achieved with pow

public class Solution {
public int Sum_Solution(int n) {
int s = (int) (Math.pow(n,2) + n);
return s>>1;
}
}

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/11236241.html