#include <stdio.h>
#include <math.h>
#include <string.h>
// return true if 2^n starts with prefix, else false
int startsWithPrefix(long long n, int prefix) {
static const double log2v = 0.30102999566398119521; // log10(2)
double x = n * log2v;
double frac = x - floor(x);
// count digits in prefix
char buf[32];
sprintf(buf, "%d", prefix);
int digits = strlen(buf);
// compute leading digits
int leading = (int)floor(pow(10.0, frac + digits - 1));
return leading == prefix;
}
int main(void) {
int prefix = 12;
long long results[3] = { 0 };
int found = 0;
for (long long n = 1; found < 3; n++) {
if (startsWithPrefix(n, prefix)) {
results[found++] = n;
}
}
printf("First 3 powers of 2 starting with %d:\n", prefix);
for (int i = 0; i < 3; i++) {
long long n = results[i];
printf("n = %lld : 2 ^ %lld = %.10g\n", n, n, pow(2.0, (double)n));
}
return 0;
}
/*
First 3 powers of 2 starting with 12:
n = 7 : 2 ^ 7 = 128
n = 80 : 2 ^ 80 = 1.20892582e+24
n = 90 : 2 ^ 90 = 1.237940039e+27
*/