Floyd's triangle: Difference between revisions

Added XBasic
m (→‎{{header|ALGOL W}}: Fixed Trans tag again)
(Added XBasic)
 
(23 intermediate revisions by 9 users not shown)
Line 130:
92 93 94 95 96 97 98 99 100 101 102 103 104 105
</pre>
 
=={{header|ABAP}}==
REPORT zmbr_test.
PARAMETERS: p_row TYPE i.
START-OF-SELECTION.
DATA(lv_column) = 0.
DATA(lv_number) = 0.
DO p_row TIMES.
lv_column += 1.
DO lv_column TIMES.
lv_number += 1.
WRITE: lv_number.
ENDDO.
WRITE:/ space.
ENDDO.
{{out}}
<pre>
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
</pre>
{{out}}
<pre>
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
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN width n:
SELECT:
n<10: RETURN 1
ELSE: RETURN 1 + width floor (n/10)
 
HOW TO DISPLAY A FLOYD TRIANGLE WITH lines LINES:
PUT lines * (lines+1)/2 IN maxno
PUT 1 IN n
FOR line IN {1..lines}:
FOR col IN {1..line}:
WRITE n >> (1 + width (maxno - lines + col))
PUT n+1 IN n
WRITE /
 
DISPLAY A FLOYD TRIANGLE WITH 5 LINES
WRITE /
DISPLAY A FLOYD TRIANGLE WITH 14 LINES
WRITE /</syntaxhighlight>
{{out}}
<pre> 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
=={{header|Action!}}==
Line 310 ⟶ 398:
 
=={{header|ALGOL W}}==
{{trans| ALGOL_68}}
<syntaxhighlight lang="algolw">begin
% prints a Floyd's Triangle with n lines %
Line 375 ⟶ 463:
92 93 94 95 96 97 98 99 100 101 102 103 104 105
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">floyd←{
max←⍵×(⍵+1)÷2
tri←↑(⍳max)⊂⍨(0,⍳max-1)∊+\0,⍳⍵
wdt←⌈⍀⊖≢∘⍕¨tri
↑,/wdt{' ',(-⍺××⍵)↑⍕⍵}¨tri
}</syntaxhighlight>
{{out}}
<syntaxhighlight lang="apl"> floyd 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
floyd 14
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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</syntaxhighlight>
 
=={{header|AppleScript}}==
Line 1,240 ⟶ 1,359:
 
=={{header|Arturo}}==
 
{{incorrect|Arturo|The last line should only have one space between values}}
<syntaxhighlight lang="rebol">floydwidth: function [rowcountrows col] .memoize [
floor 2 + log col + 1 + (rows * rows - 1) / 2 10
result: new [[1]]
]
while [rowcount > size result][
 
n: inc last last result
floyd: function [rows][
row: new []
n: 1
loop n..n+size last result 'k -> 'row ++ @[k]
row: 1
'result ++ @[row]
col: 0
while -> row =< rows [
prints pad ~"|n|" width rows col
inc 'col
inc 'n
if col = row [
print ""
col: 0
inc 'row
]
]
return result
]
 
floyd 5
loop [5 14] 'j [
print ""
f: floyd j
floyd 14</syntaxhighlight>
loop f 'row -> print map row 'r [pad to :string r 3]
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre> 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
=={{header|AutoHotkey}}==
Line 1,487 ⟶ 1,613:
92 93 94 95 96 97 98 99 100 101 102 103 104 105
</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|GW-BASIC}}
{{works with|PC-BASIC}}
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="qbasic">100 CLS : REM 100 HOME for Applesoft BASIC
110 INPUT "Number of rows: "; ROWS
120 DIM COLSIZE(ROWS)
130 FOR COL = 1 TO ROWS
140 COLSIZE(COL) = LEN(STR$(COL + ROWS * (ROWS - 1) / 2))
150 NEXT
160 THISNUM = 1
170 FOR R = 1 TO ROWS
180 FOR COL = 1 TO R
190 PRINT RIGHT$(" " + STR$(THISNUM), COLSIZE(COL)); " ";
200 THISNUM = THISNUM + 1
210 NEXT
220 PRINT
230 NEXT
240 END</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
Line 1,532 ⟶ 1,685:
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
==={{header|GW-BASIC}}===
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|IS-BASIC}}===
Line 1,567 ⟶ 1,723:
Inkey
EndOfCode</syntaxhighlight>
 
{{out}}
<pre> 1
 
1
2 3
4 5 6
Line 1,591 ⟶ 1,744:
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
==={{header|MSX Basic}}===
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|QBasic}}===
Line 1,613 ⟶ 1,769:
PRINT
FloydTriangle (14)</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">SUB floydtriangle (fila)
DIM numcolum(0)
MAT REDIM numcolum(fila)
FOR colum = 1 TO fila
LET numcolum(colum) = LEN(STR$(colum+fila*(fila-1)/2))
NEXT colum
PRINT "output for "; STR$(fila)
PRINT
LET thisnum = 1
FOR r = 1 TO fila
FOR colum = 1 TO r
PRINT (" " & STR$(thisnum))[LEN(" " & STR$(thisnum))-numcolum(colum)+1:maxnum]; " ";
LET thisnum = thisnum+1
NEXT colum
PRINT
NEXT r
END SUB
 
CALL FLOYDTRIANGLE (5)
PRINT
CALL FLOYDTRIANGLE (14)
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Floyd's triangle"
VERSION "0.0001"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION FloydTriangle (n)
 
FUNCTION Entry ()
FloydTriangle (5)
PRINT
FloydTriangle (14)
END FUNCTION
 
FUNCTION FloydTriangle (fila)
DIM numColum[fila]
FOR colum = 1 TO fila
t$ = STR$(colum + fila * (fila - 1) / 2)
numColum[colum] = LEN(t$)
NEXT colum
 
PRINT "output for "; STR$(fila)
PRINT
thisNum = 1
FOR r = 1 TO fila
FOR colum = 1 TO r
PRINT RIGHT$(" " + STR$(thisNum), numColum[colum]); " ";
INC thisNum
NEXT colum
PRINT
NEXT r
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 2,509 ⟶ 2,724:
</pre>
 
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc width(word n) word:
word w;
w := 0;
while n>0 do
w := w + 1;
n := n / 10
od;
w
corp
 
proc floyd(word rows) void:
word n, row, col, maxno;
maxno := rows * (rows+1)/2;
n := 1;
for row from 1 upto rows do
for col from 1 upto row do
write(n : 1+width(maxno - rows + col));
n := n+1
od;
writeln()
od
corp
 
proc main() void:
floyd(5);
writeln();
floyd(14)
corp</syntaxhighlight>
{{out}}
<pre> 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
=={{header|EasyLang}}==
{{trans|Java}}
<syntaxhighlight>
func ceil h .
f = floor h
if h <> f
f += 1
.
return f
.
proc triangle n . .
print n & " rows:"
row = 1
while row <= n
printme += 1
cols = ceil log10 (n * (n - 1) / 2 + nprinted + 2)
numfmt 0 cols
write printme & " "
nprinted += 1
if nprinted = row
print ""
row += 1
nprinted = 0
.
.
print ""
.
triangle 5
triangle 14
</syntaxhighlight>
{{out}}
<pre>
5 rows:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
14 rows:
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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
</pre>
 
=={{header|Elixir}}==
Line 4,481 ⟶ 4,803:
</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
floyd_t(m):=block(
t:m*(m+1)/2,
L1:makelist(makelist(k,k,1,t),j,0,m),
L2:makelist(rest(L1[i],((i-1)^2+(i-1))/2),i,1,m+1),
makelist(firstn(L2[i],i),i,1,m),
table_form(%%))$
 
/* Test cases */
floyd_t(5);
floyd_t(14);
</syntaxhighlight>
[[File:FloydTriangleMaxima5.png|thumb|center]]
[[File:FloydTriangleMaxima14.png|thumb|center]]
 
=={{header|Miranda}}==
<Syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map floyd [5, 14]))]
 
floyd :: num->[char]
floyd n = lay (map fmt rws)
where rws = rows n
cws = map ((+1).width) (last rws)
fmt rw = concat (map (uncurry rjust) (zip2 cws rw))
 
 
rows :: num->[[num]]
rows n = rows' [1..n] [1..]
where rows' [] ns = []
rows' (l:ls) ns = row : rows' ls rest
where (row, rest) = split l ns
 
split :: num->[*]->([*],[*])
split n ls = (take n ls, drop n ls)
 
rjust :: num->num->[char]
rjust w n = reverse (take w (reverse (show n) ++ repeat ' '))
 
width :: num->num
width = (#) . show
 
uncurry :: (*->**->***)->(*,**)->***
uncurry f (a,b) = f a b</syntaxhighlight>
{{out}}
<pre> 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
</pre>
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE FloydTriangle;
Line 5,193 ⟶ 5,581:
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
 
 
PUT$NUM: PROCEDURE (N, WIDTH);
DECLARE S (6) BYTE INITIAL (' $');
DECLARE N ADDRESS, (I, WIDTH) BYTE;
I = 5;
DIGIT:
S(I := I-1) = N MOD 10 + '0';
IF (N := N/10) > 0 THEN GO TO DIGIT;
DO I=I-1 TO 0 BY -1;
S(I) = ' ';
END;
CALL PRINT(.S(6-WIDTH));
END PUT$NUM;
 
WIDTH: PROCEDURE (N) BYTE;
DECLARE (N, W) BYTE;
W = 1;
DO WHILE N>0;
N = N/10;
W = W+1;
END;
RETURN W;
END WIDTH;
 
FLOYD: PROCEDURE (ROWS);
DECLARE (ROWS, ROW, COL) BYTE;
DECLARE (N, MAXNO) ADDRESS;
 
MAXNO = ROWS * (ROWS+1)/2;
N = 1;
DO ROW = 1 TO ROWS;
DO COL = 1 TO ROW;
CALL PUT$NUM(N, 1 + WIDTH(MAXNO - ROWS + COL));
N = N+1;
END;
CALL PRINT(.(13,10,'$'));
END;
END FLOYD;
 
CALL FLOYD(5);
CALL PRINT(.(13,10,'$'));
CALL FLOYD(14);
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre> 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
11
12 13
14 15 16
17 18 19 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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
=={{header|Prolog}}==
Line 5,746 ⟶ 6,206:
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Floyd 5>>
<Prout <Floyd 14>>;
};
 
Floyd {
s.N, <Rows s.N>: e.Rows,
e.Rows: e.X (e.MaxRow),
<Each Width e.MaxRow>: e.ColWidths =
<Each ((e.ColWidths)) FormatRow e.Rows>;
}
 
FormatRow {
(e.W) () = '\n';
(s.W e.WS) (s.C e.CS) = <Cell <+ 1 s.W> s.C> <FormatRow (e.WS) (e.CS)>;
};
 
Cell {
s.Width s.N, <Repeat s.Width ' '> <Symb s.N>: e.Rfill,
<Last s.Width e.Rfill>: (e.X) e.Cell = e.Cell;
}
 
Rows {
s.Rows = <Rows s.Rows 1 1>;
s.Rows s.Row s.N, <+ s.Rows 1>: s.Row = ;
s.Rows s.Row s.N, <+ s.N s.Row>: s.Next =
(<Row s.N <- s.Next 1>>)
<Rows s.Rows <+ s.Row 1> s.Next>;
}
 
Row {
s.To s.To = s.To;
s.From s.To = s.From <Row <+ s.From 1> s.To>;
};
 
Each {
s.F e.X = <Each () s.F e.X>;
(e.Arg) s.F = ;
(e.Arg) s.F t.I e.X = <Mu s.F e.Arg t.I> <Each (e.Arg) s.F e.X>;
};
 
Width {
s.N, <Symb s.N>: e.X, <Lenw e.X>: s.Width e.X = s.Width;
};
 
Repeat {
0 s.X = ;
s.N s.X = s.X <Repeat <- s.N 1> s.X>;
};</syntaxhighlight>
{{out}}
<pre> 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
=={{header|REXX}}==
===version 1===
Line 6,292 ⟶ 6,823:
92 93 94 95 96 97 98 99 100 101 102 103 104 105
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program floyd_triangle;
floyd(5);
print;
floyd(14);
print;
 
proc floyd(rows);
maxno := rows * (rows+1) div 2;
n := 1;
loop for row in [1..rows] do
loop for col in [1..row] do
nprint(lpad(str n, 1 + #str (maxno - rows + col)));
n +:=1;
end loop;
print;
end loop;
end proc;
end program;
</syntaxhighlight>
{{out}}
<pre> 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
 
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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
=={{header|Sidef}}==
Line 6,464 ⟶ 7,037:
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>
 
=={{header|Uiua}}==
Probably terrible and unidiomatic...
<syntaxhighlight lang="Uiua">
Floyd ← ⇌⍥(⊂⍚(+⇡∩(+1)⊃⧻(⊢⇌))⊢.):{[1]}-1
 
JoinUsing ← ↘1/◇⊂≡(□◇⊂)↯:⊙(⧻.)□
PadL ← ⊂/⊂↯:" "
Lengths ← ⍚∵◇∵(⧻°⋕)°□
 
PrintIt ← (
# Create table of [last row sizes] [this row sizes] [this row numbers]
⊢⊞⊂⊢⊢↙¯1.⍉[Lengths.]
 
≡(
# Calculate last row sizes - this row sizes and use to calculate paddings
⍉⊟⊃(-:↙:⊙(⧻.)∩°□°⊟↙2)(°⋕°□⊢↘2)
&pJoinUsing " " ≡(⍚PadL°⊟)
)
)
 
PrintIt Floyd 4
PrintIt Floyd 14
</syntaxhighlight>
{{out}}
<pre>
1
2 3
4 5 6
7 8 9 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
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
</pre>
 
=={{header|VBA}}==
Line 6,602 ⟶ 7,221:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var floyd = Fn.new { |n|
2,122

edits