sierpinsky.py

Created by vnap0v

Created on May 01, 2025

341 Bytes

Draws a Sierpinsky triangle on the screen. Defines one function sierp(), this function can be used with 1 parameter defining the number of iterations. Uses libraries math and kandinsky. Tested on the calculator using software version 23.2.6.


from math import *
import kandinsky
import random
def sierp(n=50000):
  cs=('red','blue','green')
  h=((160,10),(10,210),(310,210))
  px=100;py=100;oldk=0
  for i in range(n):
    k=random.randint(0,2)
    px=0.5*(px+h[k][0])
    py=0.5*(py+h[k][1])
    kandinsky.set_pixel(int(px),int(py),cs[oldk])
    oldk=k
    
sierp()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.