Skip to content

Instantly share code, notes, and snippets.

@junjihashimoto
Created October 5, 2021 04:33
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 junjihashimoto/fe75780dba77ef3e33e8be605b3cf0c5 to your computer and use it in GitHub Desktop.
Save junjihashimoto/fe75780dba77ef3e33e8be605b3cf0c5 to your computer and use it in GitHub Desktop.
Münchausen number
#include <stdio.h>
#include <stdlib.h>
const int N = 440000000;
int cache[] =
{ 0
, 1
, 4
, 27
, 256
, 3125
, 46656
, 823543
, 16777216
, 387420489
};
int
main(){
int i = 0;
int d[9];
int c[9];
for(d[8]=0;d[8]<10;d[8]++){
c[8] = cache[d[8]];
for(d[7]=0;d[7]<10;d[7]++){
c[7] = c[8] + cache[d[7]];
for(d[6]=0;d[6]<10;d[6]++){
c[6] = c[7] + cache[d[6]];
for(d[5]=0;d[5]<10;d[5]++){
c[5] = c[6] + cache[d[5]];
for(d[4]=0;d[4]<10;d[4]++){
c[4] = c[5] + cache[d[4]];
for(d[3]=0;d[3]<10;d[3]++){
c[3] = c[4] + cache[d[3]];
for(d[2]=0;d[2]<10;d[2]++){
c[2] = c[3] + cache[d[2]];
for(d[1]=0;d[1]<10;d[1]++){
c[1] = c[2] + cache[d[1]];
for(d[0]=0;d[0]<10;d[0]++){
c[0] = c[1] + cache[d[0]];
if(c[0]==i){
printf("%d\n",i);
} else if (i==N){
return 0;
}
i++;
}
}
}
}
}
}
}
}
}
return 0;
}
@junjihashimoto
Copy link
Author

$ hyperfine --warmup 2 -m 3 ./a.out
Benchmark #1: ./a.out
  Time (mean ± σ):     136.2 ms ±   2.0 ms    [User: 136.3 ms, System: 0.5 ms]
  Range (min … max):   133.0 ms … 139.9 ms    21 runs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment