Input
There's no input.
Output
Output some form of these numbers: 137, 1315, 73, 136, 255, 1384, 16385, one per line in the listed order.
Example
The first two lines of the CORRECT output file are:
137=2(2(2)+2+2(0))+2(2+2(0))+2(0)
1315=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
The correct output file should contain 7 lines.
Code
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
string expressInPowersOfTwo(unsigned long long n) {
string final = "";
if(n == 2 || n == 0) {
return final + char('0' + n);
}
bitset<64> bitRep(n);
for (int i = 63; i >= 0; --i) {
if (bitRep[i]) {
string ans = (i == 1 ? "2" :
"2(" + expressInPowersOfTwo(i) + ")");
final += (final == "" ? ans : "+" + ans);
}
}
return final;
}
int main() {
unsigned long long n;
while((cin >> n) && n != -1) {
cout << n << "=" << expressInPowersOfTwo(n) << "\n";
}
return 0;
}
Solution
137=2(2(2)+2+2(0))+2(2+2(0))+2(0)
1315=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
73=2(2(2)+2)+2(2+2(0))+2(0)
136=2(2(2)+2+2(0))+2(2+2(0))
255=2(2(2)+2+2(0))+2(2(2)+2)+2(2(2)+2(0))+2(2(2))+2(2+2(0))+2(2)+2+2(0)
1384=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2)+2(2(2)+2(0))+2(2+2(0))
16385=2(2(2+2(0))+2(2)+2)+2(0)