#include <stdio.h>
#include <string.h>
#include <math.h>

#define SIZE 32  // Size of the grid

void sierpinski(int x, int y, int size, char grid[SIZE][SIZE]) {
  if (size == 1) {
    grid[y][x] = '*';
    return;
  }

  int subSize = size / 2;

  // Top triangle
  sierpinski(x, y, subSize, grid);

  // Bottom left triangle
  sierpinski(x - subSize, y + subSize, subSize, grid);

  // Bottom right triangle
  sierpinski(x + subSize, y + subSize, subSize, grid);
}

int main() {
  char grid[SIZE][SIZE];
  memset(grid, ' ', sizeof(grid));

  sierpinski(SIZE / 2, 0, SIZE / 2, grid);

  // Calculate the height of the triangle and print only the necessary rows
  int height = (int)(0.866 * SIZE / 2);

  for (int i = 0; i <= height; i++) {
    for (int j = 0; j < SIZE; j++) {
      printf("%c", grid[i][j]);
    }
    printf("\n");
  }

  return 0;
}
