Created
September 18, 2021 22:47
-
-
Save Leowbattle/2b9093b935c616dc544c60804098593e to your computer and use it in GitHub Desktop.
Print large powers of 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <cstdint> | |
using namespace std; | |
using Digit = uint8_t; | |
using BigInt = vector<Digit>; | |
void doubleint(BigInt& num) { | |
Digit carry = 0; | |
for (int i = 0; i < num.size(); i++) { | |
Digit digit = num[i]; | |
digit = digit * 2 + carry; | |
carry = digit / 10; | |
digit = digit % 10; | |
num[i] = digit; | |
} | |
if (carry > 0) { | |
num.push_back(carry); | |
} | |
} | |
void printint(const BigInt& num) { | |
for (int i = num.size() - 1; i >= 0; i--) { | |
cout << (int)num[i]; | |
} | |
} | |
int main(int argc, char** argv) { | |
if (argc < 2) { | |
return 0; | |
} | |
int n = atoi(argv[1]); | |
BigInt num {1}; | |
for (int i = 0; i < n; i++) { | |
doubleint(num); | |
} | |
printint(num); | |
cout << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment