Skip to content

Instantly share code, notes, and snippets.

@alpheratz0
Created February 6, 2026 03:19
Show Gist options
  • Select an option

  • Save alpheratz0/c18a64133ad4e457c2542b555d193585 to your computer and use it in GitHub Desktop.

Select an option

Save alpheratz0/c18a64133ad4e457c2542b555d193585 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <limits.h>
#define Start (1)
#define Iterations (sizeof(unsigned long long)*8)
int sum_digits(unsigned long long i) {
if (i<=9) return i;
static char _buf[512] = {0};
char *p = &_buf[0];
int sum=0;
snprintf(&_buf[0], sizeof(_buf), "%llu", i);
while (*p)
sum+=(*p++-'0');
return sum_digits(sum);
}
int main(void) {
for (unsigned long long x = Start, iter = 0; iter < Iterations; ++iter, x*=2)
printf("%d\n", sum_digits(x));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment