There are two versions, but the first is merely the "better-looking" one, with unimportant squares filled in which colours instead of black. The effect of the program is to take in a number, and output either "PRIME" or "NOT PRIME" depending on which it is. The first part finds the whole number lower than the input's square root (a surprising amount of work in Piet) to save on runtime. Then, the program goes input-modulus-(2, 3, etc) to see if it has any factors. If it does, it goes through that big light magenta square to write out NOT, then into the dark cyan square. If none of the numbers below the square root are factors, it enters the dark cyan square. From there the program writes PRIME. It's strange that the text section takes up a lot more room than the calculation section.
This is similar code, but without the DP manipulation, since the black color block enforces program flow. Also included is an animated GIF consisting of 3 runnable variations of the static version. Note that the animated GIF can be run by the Piet interpreter! (Of course, I prefer the animated version.)
Program flow of these is obviously simple. They were made just to demonstrate how decorative Piet programs can be.
![]() Composition with Red, Yellow and Blue. 1921, Piet Mondrian. |
This was made with the goal of having a Piet program that really looks like a painting of Piet Mondrian. It prints "Piet".
I basically had it push 1, multiply a bunch of times (so as not to affect the stack) around the big blue block to get back to the beginning of the loop. That's where the majoriy of the steps come from. I could have had way fewer instructions by having only a few pushes/pops, but I figured I would make it pretty :-). It is a graphic language after all... I encourage everyone to try to write a Piet program, it's wonderfully mindbending.
Sylvain Tintillier provides a method of generating prime numbers using Piet. Figuring out how it works is easy, he says, "Just look at the bitmap!"
Richard Mitton supplies this amazing program which calculates an approximation of pi... literally by dividing a circular area by the radius twice.
Richard says:
The output is printed without the decimal point after the 3.Naturally, a more accurate value can be obtained by using a bigger program.
PROGRAMMING NOTES: (line numbers are my own scribbled tags, the middle is
the operation, the right column is an approximation of the stack).
01: m = in(number) m
02: n = in(number) nm
03: dup nnm
04: push 3 3nnm
05: push 1 13nnm
06: roll nmn
07: r = m % n rn
08: dup rrn
09: push 1 1rrn
10: greater 0rn
11: not 1rn
12: switch (stay left if it's not greater than 1, otherwise turn right)
rn
LEFT PATH
L1: dup rrn
GUIDE TO OLD CODE AT 03:
RIGHT PATH
R1: pop n
R2: out(number)
It's not nearly as cool as some of the other programs you have up there, but
I thought someone else might benefit from seeing a minimalistic
simple-but-documented example.
After having created the basis of the algorithm I decided to modify it in order to have a nice picture, corresponding to the notion of power. That's why the program looks like an elephant (or a pig, I must admit... :-P).
Eddy Ferreira wrote this program to help him count down the number of bottles of beer on the wall.
James Dessart designed this "Hello World" to look like a Mondrian painting. It's basically a string pushed on to the stack one character at a time,
followed by a function that prints a string off the stack. The string-printing loop involves the rainbow stripe of code near the bottom right corner.