Skip to main content
Commonmark migration
Source Link

###Example output for \$f(40,2)\$

Example output for \$f(40,2)\$

###Formula

Formula

###Commented

Commented

###Example output for \$f(40,2)\$

###Formula

###Commented

Example output for \$f(40,2)\$

Formula

Commented

saved 4 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

C (gcc),  157 153 143 139  139135 bytes

Saved several bytes thanks to @AlexeyBurdin
Saved 4 4  8 more bytes thanks to @ceilingcat

long p,a,s,c,L;f(n,d){for(a=0;a++<n;putsa=0;c=a++-n;puts(""))for(c=a+~n;p=c<a;printf;p=c<a;printf(c<0?" ":"\e[3%dm@@",L),s=++c<1?1:s*a/c-s)for(L=0;p*=d,L<7&s%p<1;L++);}

Try it online!Try it online! (no colors on TIO)

C (gcc),  157 153 143  139 bytes

Saved several bytes thanks to @AlexeyBurdin
Saved 4 more bytes thanks to @ceilingcat

long p,a,s,c,L;f(n,d){for(a=0;a++<n;puts(""))for(c=a+~n;p=c<a;printf(c<0?" ":"\e[3%dm@@",L),s=++c<1?1:s*a/c-s)for(L=0;p*=d,L<7&s%p<1;L++);}

Try it online! (no colors on TIO)

C (gcc),  157 153 143 139  135 bytes

Saved several bytes thanks to @AlexeyBurdin
Saved  4  8 more bytes thanks to @ceilingcat

long p,a,s,c,L;f(n,d){for(a=0;c=a++-n;puts(""))for(;p=c<a;printf(c<0?" ":"\e[3%dm@@",L),s=++c<1?1:s*a/c-s)for(L=0;p*=d,L<7&s%p<1;L++);}

Try it online! (no colors on TIO)

saved 4 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

C (gcc),  157 153 143  143139 bytes

Saved several bytes thanks to @AlexeyBurdin
Saved 4 more bytes thanks to @ceilingcat

long p,a,s,c,L;f(n,d){for(a=0;a++<n;puts(""))for(c=a+~n;c<a;printfc=a+~n;p=c<a;printf(c<0?" ":"\e[3%dm@@",L),s=++c<1?1:s*a/c-s)for(L=0,p=1;p*=dL=0;p*=d,L<7&&!(s%p);L++L<7&s%p<1;L++);}

Try it online!Try it online! (no colors on TIO)

long p, a, s, c, L;               // declare a few 64-bit integers
f(n, d) {                         // n = number of rows, d = colorization parameter
  for(a = 0; a++ < n; puts(""))   // for a = 1 to n, with a linefeed added after each
    for(                          // iteration:
      c = a + ~n;                 //   for c = a - n - 1 to a - 1:
      p = c < a;                      //
      printf(                     //     update the output after each iteration:
        c < 0 ?                   //       if c is negative:
          " "                     //         just append a space
        :                         //       else:
          "\e[3%dm@@",            //         append the ANSI color code, followed by '@@'
        L                         //       set the 2nd digit of the color code
      ),                          //
      s = ++c < 1 ? 1             //     increment c; set s to 1 while c is less than 1
                  : s * a / c - s //     then, update s to s * a / c - s
    )                             //
      for(                        //       compute the color L:
        L = 0,0; p = 1;                 //         start with L = 0, p = 1
        p *= d,                   //         multiply p by d
        L < 7 &&& !(s % p); < 1;        //         stop if L=7L = 7 or p does not divide s
        L++                       //         increment L
      );                          //
}                                 //

C (gcc),  157 153  143 bytes

Saved several bytes thanks to @AlexeyBurdin

long p,a,s,c,L;f(n,d){for(a=0;a++<n;puts(""))for(c=a+~n;c<a;printf(c<0?" ":"\e[3%dm@@",L),s=++c<1?1:s*a/c-s)for(L=0,p=1;p*=d,L<7&&!(s%p);L++);}

Try it online! (no colors on TIO)

long p, a, s, c, L;               // declare a few 64-bit integers
f(n, d) {                         // n = number of rows, d = colorization parameter
  for(a = 0; a++ < n; puts(""))   // for a = 1 to n, with a linefeed added after each
    for(                          // iteration:
      c = a + ~n;                 //   for c = a - n - 1 to a - 1:
      c < a;                      //
      printf(                     //     update the output after each iteration:
        c < 0 ?                   //       if c is negative:
          " "                     //         just append a space
        :                         //       else:
          "\e[3%dm@@",            //         append the ANSI color code, followed by '@@'
        L                         //       set the 2nd digit of the color code
      ),                          //
      s = ++c < 1 ? 1             //     increment c; set s to 1 while c is less than 1
                  : s * a / c - s //     then, update s to s * a / c - s
    )                             //
      for(                        //       compute the color L:
        L = 0, p = 1;             //         start with L = 0, p = 1
        p *= d,                   //         multiply p by d
        L < 7 && !(s % p);        //         stop if L=7 or p does not divide s
        L++                       //         increment L
      );                          //
}                                 //

C (gcc),  157 153 143  139 bytes

Saved several bytes thanks to @AlexeyBurdin
Saved 4 more bytes thanks to @ceilingcat

long p,a,s,c,L;f(n,d){for(a=0;a++<n;puts(""))for(c=a+~n;p=c<a;printf(c<0?" ":"\e[3%dm@@",L),s=++c<1?1:s*a/c-s)for(L=0;p*=d,L<7&s%p<1;L++);}

Try it online! (no colors on TIO)

long p, a, s, c, L;               // declare a few 64-bit integers
f(n, d) {                         // n = number of rows, d = colorization parameter
  for(a = 0; a++ < n; puts(""))   // for a = 1 to n, with a linefeed added after each
    for(                          // iteration:
      c = a + ~n;                 //   for c = a - n - 1 to a - 1:
      p = c < a;                  //
      printf(                     //     update the output after each iteration:
        c < 0 ?                   //       if c is negative:
          " "                     //         just append a space
        :                         //       else:
          "\e[3%dm@@",            //         append the ANSI color code, followed by '@@'
        L                         //       set the 2nd digit of the color code
      ),                          //
      s = ++c < 1 ? 1             //     increment c; set s to 1 while c is less than 1
                  : s * a / c - s //     then, update s to s * a / c - s
    )                             //
      for(                        //       compute the color L:
        L = 0;                    //         start with L = 0
        p *= d,                   //         multiply p by d
        L < 7 & s % p < 1;        //         stop if L = 7 or p does not divide s
        L++                       //         increment L
      );                          //
}                                 //
minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
fixed the explanation of the formula
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
added an explanation of the formula
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
saved 10 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
saved 4 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
fixed a potential bug and added a commented version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
saved 3 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
fixed the TIO link
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading