Suppose
2
a
1
+
2
a
2
+
2
a
3
+
…
+
2
a
n
=
1
6
2
7
6
2
2
where
a
1
,
a
2
,
a
3
,
…
,
a
n
are
distinct
positive integers.
What is the value of ( a 1 + a 2 + a 3 + … + a n ) + n ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
We observe that
(
2
1
+
2
2
+
2
3
.
.
.
2
n
−
1
)
+
2
=
2
n
.
Thus
2
n
is more than sum of 2's of all powers less than
n
.
Here
2
a
x
where
a
x
is maximum of all
a
's will be more than
2
1
6
2
7
6
2
2
.
Now 2 2 1 = 2 0 9 7 1 5 2 and 2 2 0 = 1 0 4 8 5 7 6 . 2 2 1 is more than the sum i.e. 1 6 2 7 6 2 2 but 2 2 0 is the maximum power of 2 less than the sum .
So there exists some a x = 2 0 for x in 1 to n .
Now we know
2
2
0
exists so we will minus that.
1
6
2
7
6
2
2
−
1
0
4
8
5
7
6
=
5
7
9
0
4
6
Similarly now we will check the max power of 2 that can be accommodated in
5
7
9
0
4
6
. It is
2
1
9
=
5
2
4
2
8
8
.
Minus that.
Now we have
5
4
7
5
8
. The max power of 2 that can be accommodated is
2
1
5
=
3
2
7
6
8
.
.....
The same process can continue and all powers of 2 that were used can be extracted.
In the sum 1627622 , the powers of 2 (
a
x
) that were used are -- 20 19 15 14 12 10 8 7 6 5 2 1 .
The
n
is 12 and the sum of
a
x
where
x
=
1
.
.
1
2
is 119 .
So answer = 131
Here is the code in AutoHotkey which I wrote to solve this problem.
msgbox % getParams(l := 1627622, 0)
msgbox % z := getParams(l, 1)
f := 0
loop, parse, z, %A_Space%
f+=A_LoopField+1
msgbox % f
return
getParams(sum, r=0){
static a := 1
while sum>0
loop
{
a*=2
if (a>sum)
{
a/=2,p.= (r ? Round(log(a)/log(2)) : Round(a)) " ",sum-=a,a:=1
break
}
}
return Substr(p,1,-1)
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Adding, we get 2 0 + 1 9 + 1 5 + 1 4 + 1 2 + 1 0 + 8 + 7 + 6 + 5 + 2 + 1 + 1 2 = 1 3 1
Here is my elegant C code to solve this problem :
int main(){
int x=1627622,i=0,s=0;
int i=0;
while (x!=0){
i++;
if (x%2==1)
s+=i;
x/=2;
}
printf("%d",s);
}
Explanation : We need to convert 1627622 to binary , then we have to count the sum of ones in its binary representation added to the sum of their positions in the representation , in other words : their powers . Since each time we find a one we add to our sum its position plus one for its presence : s+=i+1 , or we can , as I wrote in my code , initiate the first value of i to 0 and increment it as we enter the while loop , so the counting starts with 1.
We are being asked to express this number as a sum of base two logarithms. It's easy to find that 2 2 2 exceeds the number. Therefore, what we need to do is to start with 2 2 2 and remove successively smaller powers of two, if present in the number, and noting their presence. Here is one possible way of doing that.
>>> indices = [ ]
>>> r=1627622
>>> for k in range ( 22, -1, -1 ) :
... term = 2 ** k
... if term < r :
... r -= term
... indices. append(k)
... elif term == r :
... indices.append(k)
... break
... else :
... pass
...
>>> indices
[20, 19, 15, 14, 12, 10, 8, 7, 6, 5, 2, 1]
>>> sum+len(indices)
131
Problem Loading...
Note Loading...
Set Loading...
converting 1627622 to binary 110001101010111100110 , now expanding this binary form in powers of 2 , we get it in same form as given question where
a1 =20
a2 =19
a3 =15
a4 =14
a5 =12
a6 = 10
a7 = 8
a8 = 7
a9 = 6
a10 = 5
a11 = 2
a12 = 1
hence ΣAn + n = 119 + 12 = 131