Uploaded by drashtipatel72

HW#1

advertisement
Chapter One:
Homework1 : Page 26: 1.5, 1.12
1.5 Write a recursive method that returns the number of 1’ s in the binary representation of N.
Use the fact that this is equal to the number of 1’ s in the representation of N/ 2, plus 1, if N is
odd.
#include <iostream>
using namespace std;
int rec(int n)
{
if (n==0)
return 0;
if((n%2)==0)
return rec(n/2);
else
return 1+ rec(n/2);
}
int main()
{
int x=1;
cout << rec(x) << endl;
return 0;
}
1.12 Prove the following formulas:
2
a. ∑𝑁
𝑖=1(2𝑖 − 1) = 𝑁
𝑁
3
2
b. ∑𝑁
𝑖=1 𝑖 = (∑𝑖=1 𝑖 ))
https://math.stackexchange.com/questions/62171/proving -13-23-cdots-n3-leftfracnn12-right2-using-induct
Download