I am beginner in programming and I'm learning a c++ language.
I have to create a program, that firstly load a amount of numbers and after this load numbers. I need to obtain a result that adds number one by one if they are not power of 2, else I need to subtract this number.
For example: for 3, result is = -1 - 2 + 3 = 0 (1 and 2 are powers of 2) for 4, result is = -1 - 2 + 3 - 4 = -4
It sounds that it's not problem, but the max value, that program must convert into result is 1 000 000 000. And it must give a result in 3 seconds.
I am pasting my code below. Please, help me make this with ability of converting huge numbers in short time.
#include <iostream>
using namespace std;
int main()
{
long long amount, number, i = 0, a, b, result = 0;
cin >> amount;
while (i < amount) {
cin >> number;
if (number < 100000000) {
a = 1;
result = 0;
} else if (100000000 <= number) {
a = 100000001;
result = 4999999791564546;
}
while (a <= number) {
b = a;
while (b % 2 == 0 && b > 1) {
b = b / 2;
}
if (b == 1) {
result = result - a;
} else {
result = result + a;
}
a++;
}
cout << result << endl;
i++;
}
}