Skip to content

Latest commit

 

History

History
84 lines (67 loc) · 2.6 KB

README.md

File metadata and controls

84 lines (67 loc) · 2.6 KB

Sierpinski Pattern Challenges

Feel free to use helper functions and whatever is needed!

Forbidden:

  • ChatGPT/CoPilot
  • StackOverflow etc

Hints:

Step 1 Sierpinski triangle n=1

Write a Java program to draw Triangle as below.

drawTriangle(4);

Output:
   *   
  * *  
 *   * 
*******

Step 2: Sierpinski Carpet

Implement a Java program to generate and display a Sierpinski Carpet using recursion. The provided code gives you a starting point with a function drawSierpinskiCarpet that takes two parameters - order and size. The order determines the level of recursion, and size sets the size of the carpet. The program initializes a 2D char array representing the carpet, fills it with empty spaces, and then populates it with '*' characters based on the Sierpinski pattern.

Output for drawSierpinskiCarpet(3, 27)
***************************
* ** ** ** ** ** ** ** ** *
***************************
***   ******   ******   ***
* *   * ** *   * ** *   * *
***   ******   ******   ***
***************************
* ** ** ** ** ** ** ** ** *
***************************
*********         *********
* ** ** *         * ** ** *
*********         *********
***   ***         ***   ***
* *   * *         * *   * *
***   ***         ***   ***
*********         *********
* ** ** *         * ** ** *
*********         *********
***************************
* ** ** ** ** ** ** ** ** *
***************************
***   ******   ******   ***
* *   * ** *   * ** *   * *
***   ******   ******   ***
***************************
* ** ** ** ** ** ** ** ** *
***************************

Step 3: Sierpinski Triangle Implementation

Write a Java program to draw a Sierpinski Triangle Implement the drawSierpinskiTriangle function that takes an integer parameter n representing the order of the triangle. The function should draw an equilateral triangle of size 2^n - 1 using '*' characters. The triangle should be centered in the console. The program should have a main method to demonstrate the function.

drawSierpinskiTriangle(3);

Output:
    *           
    **          
                
   *  *         
   ** **        
                
                
  *     *       
 **     **                      
*  *   *  *     
** **  ** **    

The output does not need to be exactly the same. The closer to the best results the better!