Skip to content

Instantly share code, notes, and snippets.

@Leowbattle
Created September 18, 2021 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leowbattle/2b9093b935c616dc544c60804098593e to your computer and use it in GitHub Desktop.
Save Leowbattle/2b9093b935c616dc544c60804098593e to your computer and use it in GitHub Desktop.
Print large powers of 2
#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