/prog/ - Programming General

Discuss technology, especially things related to free software, self-hosting, privacy, and anything in the associated Kaczynskiverse.
Post Reply
User avatar
Thricegreat
Posts: 171
Joined: Tue Jun 09, 2026 9:30 am
Has thanked: 162 times
Been thanked: 210 times
Contact:

/prog/ - Programming General

Post by Thricegreat »

Discuss about what to program and how to program.
User avatar
Thricegreat
Posts: 171
Joined: Tue Jun 09, 2026 9:30 am
Has thanked: 162 times
Been thanked: 210 times
Contact:

Re: /prog/ - Programming General

Post by Thricegreat »

I would start with a lightweight topic everyone knows: FizzBuzz.

This is my version of it I once posted on my webpage. Which was the only programming post of mine. I think you can find this answer pretty much anywhere too, but the point is perceiving it as using Z15 instead of Z3 + Z5. Because those are isomorphic.

`````````````````````````````````````````````````````````````````````````````

Code: Select all

#include <stdio.h>

const char *ARRAY[15] = {
    "FizzBuzz", "", "", "Fizz", "",
    "Buzz", "Fizz", "", "", "Fizz",
    "Buzz", "", "Fizz", "", ""
};

int main(void) {
    int num, i;

    printf("Num? ");
    scanf("%d", Num);

    for (i=1; i<=num; ++i) {
        if (ARRAY[i%15] == "")
            printf("%d\n", i);
        else
            printf("%s\n", ARRAY[i%15]);
    }

    return 0;
}
`````````````````````````````````````````````````````````````````````````````

P.S. It seems I can't show multiple spaces or tabs.
-- Thank you satoridepon. :)
Last edited by Thricegreat on Sat Jun 20, 2026 3:58 pm, edited 6 times in total.
User avatar
satoridepon
Posts: 29
Joined: Fri May 29, 2026 11:09 pm
Has thanked: 79 times
Been thanked: 59 times

Re: /prog/ - Programming General

Post by satoridepon »

Thricegreat wrote: Sat Jun 20, 2026 8:05 am
P.S. It seems I can't show multiple spaces or tabs.
You need to surround it with [c0de][/code]
:waha:
User avatar
drummyfish
Posts: 103
Joined: Wed May 27, 2026 1:10 pm
Location: Moravia (Czech Republic)
Has thanked: 167 times
Been thanked: 170 times
Contact:

Re: /prog/ - Programming General

Post by drummyfish »

Thricegreat wrote: Sat Jun 20, 2026 8:05 am
I would start with a lightweight topic everyone knows: FizzBuzz.

This is my version of it I once posted on my webpage. Which was the only programming post of mine. I think you can find this answer pretty much anywhere too, but the point is that perceiving it as using Z15 instead of Z3 + Z5. Because those are isomorphic.

`````````````````````````````````````````````````````````````````````````````

Code: Select all

#include <stdio.h>

const char *ARRAY[15] = {
    "FizzBuzz", "", "", "Fizz", "",
    "Buzz", "Fizz", "", "", "Fizz",
    "Buzz", "", "Fizz", "", ""
};

int main(void) {
    int num, i;

    printf("Num? ");
    scanf("%d", Num);

    for (i=1; i<=num; ++i) {
        if (ARRAY[i%15] = "")
            printf("%d\n", i);
        else
            printf("%s\n", ARRAY[i%15]);
    }

    return 0;
}
`````````````````````````````````````````````````````````````````````````````

P.S. It seems I can't show multiple spaces or tabs.
-- Thank you satoridepon. :)
Nice, a mathematician's approach. I think instead of 'if (ARRAY[i%15] = "")' you maybe want 'if (ARRAY[i%15][0] == 0)'?

I tried writing FizzBuzz in different ways for my wiki. Here's what I came up with.

This is like the "normal", basic solution:
Spoiler

Code: Select all

#include <stdio.h>

int main(void)
{
  for (int i = 1; i <= 100; ++i)
  {
    if (i != 1)
      printf(", ");

    if (i % 3 == 0 && i % 5 == 0)
      printf("FizzBuzz");
    else if (i % 3 == 0) // checking divisibility by 3 again :/
      printf("Fizz");
    else if (i % 5 == 0) // checking divisibility by 5 again :/
      printf("Buzz");
    else
      printf("%d",i);
  }

  putchar('\n');
  return 0;
}
Here with a little "optimization" to remove redundant divisibility checks (division is generally expensive):
Spoiler

Code: Select all

#include <stdio.h>

int main(void)
{
  for (int i = 1; i <= 100; ++i)
  {
    if (i != 1)
      printf(", ");

    int printNum = 1;

    if (i % 3 == 0)
    {
      printf("Fizz");
      printNum = 0;
    }

    if (i % 5 == 0)
    {
      printf("Buzz");
      printNum = 0;
    }

    if (printNum)
      printf("%d",i);
  }

  putchar('\n');
  return 0;
}
Now here we're making it a lot nicer, shorter but even more faster, and this is similar to your approach:
Spoiler

Code: Select all

#include <stdio.h>

int main(void)
{
  for (int i = 1; i <= 100; ++i)
  {
    if (i != 1)
      printf(", ");

    switch ((i % 3 == 0) + ((i % 5 == 0) << 1))
    {
      case 1: printf("Fizz"); break;
      case 2: printf("Buzz"); break;
      case 3: printf("FizzBuzz"); break;
      default: printf("%d",i); break;
    }

  }

  putchar('\n');
  return 0;
}
And here's something I came up with later, it's based on sieve of Eratosthenes AND doesn't need any division at all :varg: (edited to a better version now)
Spoiler

Code: Select all

#include <stdio.h>

int main(void)
{
  int next3Mult = 3, next5Mult = 5;

  for (int i = 1; i <= 100; ++i)
  {
    int printNum = 1;

    if (i > 1)
      printf(", ");

    if (i == next3Mult)
    {
      printf("Fizz");
      next3Mult += 3;
      printNum = 0;
    }

    if (i == next5Mult)
    {
      printf("Buzz");
      next5Mult += 5;
      printNum = 0;
    }

    if (printNum)
      printf("%d",i);
  }

  return 0;
}
Now for the lulz: code with zero branching:
Spoiler

Code: Select all

#include <stdio.h>

char str[] = "\0\0\0\0\0\0\0\0Fizz\0\0\0\0Buzz\0\0\0\0FizzBuzz";
char *comma = ", ";

int main(void)
{
  for (int i = 1; i <= 100; ++i)
  {
    // look mom, no branches!
    char *s = str;

    printf(comma + 2 * (i == 1));

    *s = '1'; // convert number to string
    s += i >= 100;
    *s = '0' + (i / 10) % 10;
    s += (*s != '0') | (i >= 100);
    *s = '0' + i % 10;

    int offset = ((i % 3 == 0) + ((i % 5 == 0) << 1)) << 3;
    printf(str + offset);
  }

  putchar('\n');
  return 0;
}
And for fun again: code golfer edition. If you can minimize it further, pls post :happy: (EDIT: minimized a little more)
Spoiler

Code: Select all

#include <stdio.h>
int i;main(){for(;i++<100;printf(", "+2*(i<2))&printf("%d\0FizzBuzz\0Fizz"+!(i%3)*12+!(i%5)*7%16,i));}
Last edited by drummyfish on Mon Jun 22, 2026 12:00 pm, edited 1 time in total.
love everyone, help selflessly
User avatar
drummyfish
Posts: 103
Joined: Wed May 27, 2026 1:10 pm
Location: Moravia (Czech Republic)
Has thanked: 167 times
Been thanked: 170 times
Contact:

Re: /prog/ - Programming General

Post by drummyfish »

Thricegreat I've taken inspiration in your approach and wrote my own version of it to add to my fizzbuzz collection, thank you for the idea. It uses no division, I think it's a very cool solution overall.

Code: Select all

#include <stdio.h>

#define F "Fizz"
#define B "Buzz"
#define FB "FizzBuzz"
#define N "%d"

static const char *pattern[15] = {N,N,F,N,B,F,N,N,F,B,N,F,N,N,FB};

int main(void)
{
  int p = 0;

  for (int i = 1; i <= 100; ++i)
  {
    if (i > 1)
      printf(", ");

    printf(pattern[p],i);

    p = p < 14 ? p + 1 : 0;
  }

  return 0;
}
love everyone, help selflessly
User avatar
Thricegreat
Posts: 171
Joined: Tue Jun 09, 2026 9:30 am
Has thanked: 162 times
Been thanked: 210 times
Contact:

Re: /prog/ - Programming General

Post by Thricegreat »

drummyfish wrote: Sun Jun 21, 2026 8:23 am
Thricegreat I've taken inspiration in your approach and wrote my own version of it to add to my fizzbuzz collection, thank you for the idea. It uses no division, I think it's a very cool solution overall.

Code: Select all

#include <stdio.h>

#define F "Fizz"
#define B "Buzz"
#define FB "FizzBuzz"
#define N "%d"

static const char *pattern[15] = {N,N,F,N,B,F,N,N,F,B,N,F,N,N,FB};

int main(void)
{
  int p = 0;

  for (int i = 1; i <= 100; ++i)
  {
    if (i > 1)
      printf(", ");

    printf(pattern[p],i);

    p = p < 14 ? p + 1 : 0;
  }

  return 0;
}
That is great. A nice job.

To note anything, in my mind it was a structure rather than a pattern. Since I think natural numbers are as real as my hands, I regard Z15 as an independently existing structure in the real world. So when I read "pattern" it felt so weird.

But that is just my autistic screeching. And I don't think this thread is the place to talk about matters like this. So feel free about that. Your version is better than mine anyway.
User avatar
drummyfish
Posts: 103
Joined: Wed May 27, 2026 1:10 pm
Location: Moravia (Czech Republic)
Has thanked: 167 times
Been thanked: 170 times
Contact:

Re: /prog/ - Programming General

Post by drummyfish »

Thricegreat wrote: Sun Jun 21, 2026 1:23 pm
To note anything, in my mind it was a structure rather than a pattern. Since I think natural numbers are as real as my hands, I regard Z15 as an independently existing structure in the real world. So when I read "pattern" it felt so weird.
It's a great topic for discussion in math general or even philosophy general maybe, feel free to bring it up. In this thread it's just an identifier name I guess, I chose it without thinking. "Structure" in this context tends to invoke "data structure" or something that could be confusing. But if you ask me to take off my engineer hat and put on a philosopher hat, I'll agree with you, I choose to think of mathematical structures as perfectly real, just abstract. A starting point I found on Wikipedia can perhaps be this.
love everyone, help selflessly
Post Reply