Let the function O(n) denote the sum of natural numbers, i, less than or equal to n. However, this function has one trick - if the number to be added, i, is a power of 2, then instead of adding we subtract the number.
Find O(4)+O(5)+O(6)+...+O(10000)
If the series had begun O(1)+..., [the letter O of the puzzle] a 1 would be contributing to each of 10000 members of the sum, and 2 would be contributing to 9999 and a 3 to 9998 of the summands, etc.
However a 1, a 2 and a 3 contribute to only 9997 of the summands. The number 4 though contributes fully its expected 9997 times; 5 contributes to 9996, etc.
We need the total of:
-1 * 9997
-2 * 9997
3 * 9997
-4 * 9997
5 * 9996
6 * 9995
7 * 9994
-8 * 9993
...
9999 * 2
10000 * 1
Note the negative contributions of all the powers of 2 starting with 1 = 2^0.
DefDbl A-Z
Dim crlf$
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
toto = 0: nxtpwr2 = 1
For i = 1 To 10000
If i < 4 Then
amt = i * 9997
Else
amt = i * (10001 - i)
End If
If i = nxtpwr2 Then
toto = toto - amt
nxtpwr2 = nxtpwr2 * 2
Else
toto = toto + amt
End If
DoEvents
Next
Text1.Text = Text1.Text & toto & " done"
DoEvents
End Sub
gives
166,567,934,208
|
Posted by Charlie
on 2019-10-10 12:08:48 |