Note: It has been reported that this program may be buggy; it goes into an infinite loop when traced with nPiet.
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".Note: It has been reported that this program may be buggy; it goes into an infinite loop when traced with nPiet.
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.
Kyle Woodward wrote this version of a prime number tester, designed to look like a Mondrian painting. The actual size version cleverly hides some intricate details in single pixels, as seen in the enlarged version.
? 2007The program will then print out the day of week in the range: 0 = Sun, 1 = Mon, etc. through to 6 = Sat. In the example execution above, the program has computed that March 15th, 2007 is a Sunday.
? 3
? 15
0
These are actually two different programs written by Sergei Lewis using the
Piet assembler he wrote. The large one is the most complex Piet program
I've seen yet.
The smaller program implements counting according to the Fizz Buzz game, in which multiples of 3 are replaced by the word "fizz", multiples of 5 are replaced by the word "buzz", and multiples of 15 are replaced by "fizz-buzz".
The larger program is... wait for it... a text adventure game.
Matthias Ernst has done the amazing and implemented an entire other Turing-complete esoteric programming language in Piet! Here is his
brainfuck interpreter. Matthias writes:
The Piet program takes a Brainfuck program, and its input (seperated by |), from STDIN and prints the output of the Brainfuck program to STDOUT. E.g. the input ",+>,+>,+>,+.<.<.<.|sdhO" would generate the output "Piet".I've also written a PHP script that generates those Brainfuck interpreters with hard coded Brainfuck code, so they just need the input of the Brainfuck program. So the script can also be called "Brainfuck2Piet converter". The script is available here. (There's also an explanation of the functional principle in general.)
Antoine Lucas has done a "cowsay" program, without using any of the pastel colours. Sample input and output:
$ echo "hello world" | npiet untitled.ppm
_____________
< hello world >
-------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
$ echo "Legi, Patres colendissimi, in Arabum monumentis, interrogatum
Abdalam 1 Sarracenum, quid in hac quasi mundana scaena admirandum
maxime spectaretur, nihil spectari homine admirabilius respondisse." |
npiet untitled.ppm
_________________________________________
/ Legi, Patres colendissimi, in Arabum mo \
| numentis, interrogatum Abdalam 1 Sarrac |
| enum, quid in hac quasi mundana scaena |
| admirandum maxime spectaretur, nihil sp |
\ ectari homine admirabilius respondisse. /
-----------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
npiet -cs 2 helloworld-piet.gif
Piet
npiet -cs 1 helloworld-piet.gif
Hello world!
Geerten Vink has implemented Conway's Game of Life. He says:
The grid is 10×10, with the x-axis running from right to left. The options menu gives you the opportunity to "TOGGLE" cells on or off, "QUIT", or take a simulation "STEP".
Greg Hendrickson has composed a Valentine's Day card for his beloved in Piet. Awwww...
Click on the large image for a larger image showing the code trace and an explanation of how the code works.
Note: The Piet code image was output with black lines between codels, then rescaled to fit on this page. Because of the black lines, it can't be used directly as actual Piet code without resampling the codels.