Floyd's triangle: Difference between revisions
Content deleted Content added
→{{header|Pascal}}: fixed output format. |
|||
Line 251: | Line 251: | ||
92 93 94 95 96 97 98 99 100 101 102 103 104 105 </pre> |
92 93 94 95 96 97 98 99 100 101 102 103 104 105 </pre> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{incorrect|Pascal|Layout of columns is wrong. there are two spaces between 94 and 95 for example}} |
|||
{{works with|Free_Pascal}} |
{{works with|Free_Pascal}} |
||
<lang pascal>Program FloydDemo (input, output); |
<lang pascal>Program FloydDemo (input, output); |
||
function digits(number: integer): integer; |
|||
begin |
|||
digits := trunc(ln(number) / ln(10)) + 1; |
|||
end; |
|||
procedure floyd1 (numberOfLines: integer); |
procedure floyd1 (numberOfLines: integer); |
||
{ variant with repeat .. until loop } |
{ variant with repeat .. until loop } |
||
var |
var |
||
i, j, |
i, j, numbersInLine, startOfLastlLine: integer; |
||
begin |
begin |
||
startOfLastlLine := (numberOfLines - 1) * numberOfLines div 2 + 1; |
|||
i := 1; |
i := 1; |
||
j := 1; |
j := 1; |
||
Line 267: | Line 271: | ||
repeat |
repeat |
||
repeat |
repeat |
||
write(i: digits, ' '); |
write(i: digits(startOfLastlLine - 1 + j), ' '); |
||
inc(i); |
inc(i); |
||
inc(j); |
inc(j); |
||
Line 276: | Line 280: | ||
until (numbersInLine > numberOfLines); |
until (numbersInLine > numberOfLines); |
||
end; |
end; |
||
procedure floyd2 (numberOfLines: integer); |
procedure floyd2 (numberOfLines: integer); |
||
{ Variant with for .. do loop } |
{ Variant with for .. do loop } |
||
var |
var |
||
i, j, |
i, j, numbersInLine, startOfLastlLine: integer; |
||
begin |
begin |
||
startOfLastlLine := (numberOfLines - 1) * numberOfLines div 2 + 1; |
|||
i := 1; |
i := 1; |
||
for numbersInLine := 1 to numberOfLines do |
for numbersInLine := 1 to numberOfLines do |
||
Line 289: | Line 293: | ||
for j := 1 to numbersInLine do |
for j := 1 to numbersInLine do |
||
begin |
begin |
||
write(i: digits, ' '); |
write(i: digits(startOfLastlLine - 1 + j), ' '); |
||
inc(i); |
inc(i); |
||
end; |
end; |
||
Line 295: | Line 299: | ||
end; |
end; |
||
end; |
end; |
||
begin |
begin |
||
writeln ('*** Floyd 5 ***'); |
|||
floyd1(5); |
floyd1(5); |
||
writeln; |
|||
writeln ('*** Floyd 14 ***'); |
|||
floyd2(14); |
floyd2(14); |
||
end.</lang> |
end.</lang> |
||
Output: |
Output: |
||
<pre>% ./Floyd |
<pre>% ./Floyd |
||
*** Floyd 5 *** |
|||
⚫ | |||
2 3 |
|||
4 5 6 |
|||
7 8 9 10 |
|||
11 12 13 14 15 |
|||
*** Floyd 14 *** |
|||
1 |
1 |
||
2 3 |
2 3 |
||
Line 307: | Line 322: | ||
7 8 9 10 |
7 8 9 10 |
||
11 12 13 14 15 |
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> |
|||
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|Python}}== |
=={{header|Python}}== |