Floyd’s Triangle
May 22, 2018
Our first solution uses two nested loops; the loop on i runs from 0 to the size of the triangle, and the loop on j runs from 0 to i:
(define (floyd1 n) (let ((num 1)) (do ((i 0 (+ i 1))) ((<= n i)) (do ((j 0 (+ j 1))) ((< i j)) (display num) (display (if (< j i) #\space #\newline)) (set! num (+ num 1))))))
> (floyd1 10) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Our second solution has a single loop that runs from 1 to the highest number to be printed, stopping when the correct number of rows has been printed:
(define (floyd2 n) (let ((row 0)) (do ((num 1 (+ num 1))) ((= row n)) (display num) (if (square? (+ (* 8 num) 1)) (begin (display #\newline) (set! row (+ row 1))) (display #\space)))))
> (floyd2 10) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
This version of the program identifies the last number on each row as a triangle number (the fifth triangle number, 15, is the number of balls that form a triangle with sides of length 5, like billiard balls) where n is triangular if and only if 8n + 1 is a perfect square.
You can run the program at https://ideone.com/MFK97F.
Golfing these in perl – the first one uses map to perform two loops..; the second a single loop – until we start drawing the “n+1″st row..
Perl6 solution using a single while loop and two variables to track state:
Alternative Perl6 solution that recursively works backward from the nth triangle number:
For most favoured languages try
Here’s a solution in C that uses a nested loop along with a counter.
Example:
Here’s a solution in C that uses a counter, printing a newline each time a triangular number is reached.
Example:
Here’s a solution in Python 3.
Output:
Prints the nth line of the triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 5
Two solutions in Haskell.
The first one starts with the infinite list of successive integers and chops it up in increasingly larger chunks
The second solution generates increasingly long lists
The triangles can be printed using the following code:
If you don’t want to print an infinite triangle, you can cut it short as follows:
Racket
All three versions produce the same output. Here an example for n = 10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
Code 👆 in Ruby.
Bash.
1 loop
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
2 loops
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Clojure/Script lazy streams
Test (try online at http://clojurescript.net):
[1]
[2 3]
[4 5 6]
[7 8 9 10]
[11 12 13 14 15]
Logically equivalent imperative Bash version using arrays
Test:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Haskell. Infinite list generator the first:
The second, using an unfold:
Either can then be pretty-printed to an arbitrary number of rows as follows: