1^2+2^2+...+100^2=100*(100+1)*(2*100+1)/6=100*101*201/6=50*101*67=(49+1)*(98+3)*(63+4)
所以,上式除以7的余数为1*3*4除以7的余数,即为5。
VB代码。调试环境:WIN2K+VB6
Private Sub Command1_Click()
Dim x As Long
x = 0
For i = 1 To 100
DoEvents
x = x + i ^ 2
Next i
MsgBox x Mod 7
End Sub
int main(){
long sum=0;
for (int i=1;i<=100;i++)
sum+=i*i;
return sum % 7;
}