graphicalSieve.frink

Download or view graphicalSieve.frink in plain text format


// Graphical Sieve of Eratosthenes
// Demonstrates dynamical repaint.  This is not necessarily an efficient way
// to do things, but demonstrates that the graphics can be dynamically updated
// as more data is added.

n = eval[input["Enter highest number: ", 10000]]
sieve[n,floor[sqrt[n]]]

sieve[n, cols] :=
{
   g = new graphics
   g.antialiased[false]
   g.color[0,0,0]
   g.fillRectSides[0,0,cols,n div cols]

   win = g.show[]
   
   // Initialize array
   array = array[0 to n]
   array@1 = false

   g.color[1,1,1]
   for i = 2 to ceil[sqrt[n]]
      if array@i != 0
      {
         for j = i^2 to n step i
         {
            array@j = 0
            x = j mod cols
            y = j div cols
            g.fillRectSides[x,y,x+1,y+1]
         }
         win.repaint[]
      }
//   print["About to paint."]
//   g.print[]
}


Download or view graphicalSieve.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19945 days, 16 hours, 7 minutes ago.