求平方和0-1+ 4-9 +16-25+36 -49+64-81-100
2019-01-08求平方和0-1+ 4-9 +16-25+36 -49+64-81-100
1、设sign=-1,如果单数就乘-1
2、例如
#!/bin/sh
if [[ -z $1 ]];then
echo Usage $0 Num
exit 0
fi
N=$1
sum=0
sign=-1
for((i=0;i<=${N};i++));do
((j=i*i))
if(( i%2 ));then
((j=j*sign))
fi
((sum=sum+j))
done
echo Sum is: $sum
3、./sum.sh 10
Sum is: 55
4、
#!/usr/bin/python
Sum=0
sign=-1
def recursion(n):
return n*n
for i in range(0,11):
j=recursion(i)
if i%2 :
j=recursion(i)*sign
Sum=Sum+j
print(Sum)
分类:Linux | 标签: |