2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,14 +1,19 @@
|
|||
[[wp:Floyd's triangle|Floyd's triangle]] lists the natural numbers in a right triangle aligned to the left where
|
||||
* the first row is just 1
|
||||
[[wp:Floyd's triangle|Floyd's triangle]] lists the natural numbers in a right triangle aligned to the left where
|
||||
* the first row is '''1''' (unity)
|
||||
* successive rows start towards the left with the next number followed by successive naturals listing one more number than the line above.
|
||||
|
||||
|
||||
The first few lines of a Floyd triangle looks like this:
|
||||
<pre> 1
|
||||
<pre>
|
||||
1
|
||||
2 3
|
||||
4 5 6
|
||||
7 8 9 10
|
||||
11 12 13 14 15</pre>
|
||||
11 12 13 14 15
|
||||
</pre>
|
||||
|
||||
The task is to:
|
||||
# Write a program to generate and display here the first n lines of a Floyd triangle.<br>(Use n=5 and n=14 rows).
|
||||
# Ensure that when displayed in a monospace font, the numbers line up in vertical columns as shown and that only one space separates numbers of the last row.
|
||||
|
||||
;Task:
|
||||
:# Write a program to generate and display here the first n lines of a Floyd triangle. <br>(Use n=5 and n=14 rows).
|
||||
:# Ensure that when displayed in a mono-space font, the numbers line up in vertical columns as shown and that only one space separates numbers of the last row.
|
||||
<br><br>
|
||||
|
|
|
|||
20
Task/Floyds-triangle/CoffeeScript/floyds-triangle.coffee
Normal file
20
Task/Floyds-triangle/CoffeeScript/floyds-triangle.coffee
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
triangle = (array) -> for n in array
|
||||
console.log "#{n} rows:"
|
||||
printMe = 1
|
||||
printed = 0
|
||||
row = 1
|
||||
to_print = ""
|
||||
while row <= n
|
||||
cols = Math.ceil(Math.log10(n * (n - 1) / 2 + printed + 2.0))
|
||||
p = ("" + printMe).length
|
||||
while p++ <= cols
|
||||
to_print += ' '
|
||||
to_print += printMe + ' '
|
||||
if ++printed == row
|
||||
console.log to_print
|
||||
to_print = ""
|
||||
row++
|
||||
printed = 0
|
||||
printMe++
|
||||
|
||||
triangle [5, 14]
|
||||
16
Task/Floyds-triangle/Kotlin/floyds-triangle.kotlin
Normal file
16
Task/Floyds-triangle/Kotlin/floyds-triangle.kotlin
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
fun main(args: Array<String>) = args.forEach { Triangle(it.toInt()) }
|
||||
|
||||
internal class Triangle(n: Int) {
|
||||
init {
|
||||
println("$n rows:")
|
||||
var printMe = 1
|
||||
var printed = 0
|
||||
var row = 1
|
||||
while (row <= n) {
|
||||
val cols = Math.ceil(Math.log10(n * (n - 1) / 2 + printed + 2.0)).toInt()
|
||||
print("%${cols}d ".format(printMe))
|
||||
if (++printed == row) { println(); row++; printed = 0 }
|
||||
printMe++
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Task/Floyds-triangle/Maple/floyds-triangle.maple
Normal file
31
Task/Floyds-triangle/Maple/floyds-triangle.maple
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
floyd := proc(rows)
|
||||
local num, numRows, numInRow, i, digits;
|
||||
digits := Array([]);
|
||||
for i to 2 do
|
||||
num := 1;
|
||||
numRows := 1;
|
||||
numInRow := 1;
|
||||
while numRows <= rows do
|
||||
if i = 2 then
|
||||
printf(cat("%", digits[numInRow], "a "), num);
|
||||
end if;
|
||||
num := num + 1;
|
||||
if i = 1 and numRows = rows then
|
||||
digits(numInRow) := StringTools[Length](convert(num-1, string));
|
||||
end if;
|
||||
if numInRow >= numRows then
|
||||
if i = 2 then
|
||||
printf("\n");
|
||||
end if;
|
||||
numInRow := 1;
|
||||
numRows := numRows + 1;
|
||||
else
|
||||
numInRow := numInRow +1;
|
||||
end if;
|
||||
end do;
|
||||
end do;
|
||||
return NULL;
|
||||
end proc:
|
||||
|
||||
floyd(5);
|
||||
floyd(14);
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
options replace format comments java crossref symbols binary
|
||||
/*REXX program constructs & displays Floyd's triangle for any number of rows.*/
|
||||
parse arg numRows .
|
||||
if numRows == '' then numRows = 1 -- assume 1 row is not given
|
||||
if numRows == '' then numRows = 1 -- assume 1 row if not given
|
||||
maxVal = numRows * (numRows + 1) % 2 -- calculate the max value.
|
||||
say 'displaying a' numRows "row Floyd's triangle:"
|
||||
say
|
||||
|
|
|
|||
1
Task/Floyds-triangle/Perl-6/floyds-triangle-1.pl6
Normal file
1
Task/Floyds-triangle/Perl-6/floyds-triangle-1.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
constant @floyd = (1..*).rotor(1..*);
|
||||
1
Task/Floyds-triangle/Perl-6/floyds-triangle-2.pl6
Normal file
1
Task/Floyds-triangle/Perl-6/floyds-triangle-2.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
constant @floyd = gather for 1..* -> $s { take [++$ xx $s] }
|
||||
|
|
@ -1,5 +1,3 @@
|
|||
constant @floyd = gather for 1..* -> $s { take [++$ xx $s] }
|
||||
|
||||
sub say-floyd($n) {
|
||||
my @formats = @floyd[$n-1].map: {"%{.chars}s"}
|
||||
|
||||
|
|
@ -1,11 +1,12 @@
|
|||
/*REXX pgm constructs & displays Floyd's triangle for any number of rows*/
|
||||
parse arg rows .; if rows=='' then rows=5 /*Not specified? Use default*/
|
||||
maxV = rows * (rows+1) % 2 /*calculate the max value. */
|
||||
say 'displaying a' rows "row Floyd's triangle:"; say /*show header*/
|
||||
#=1; do r=1 for rows; i=0; _= /*row by row.*/
|
||||
do #=# for r; i=i+1 /*start a row*/
|
||||
_ = _ right(#, length(maxV-rows+i)) /*build a row*/
|
||||
end /*#*/ /*row is done*/
|
||||
say substr(_,2) /*suppress the 1st leading blank,*/
|
||||
end /*r*/ /* [↑] introduced by 1st abutt.*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
/*REXX program constructs & displays Floyd's triangle for any number of specified rows.*/
|
||||
parse arg rows .; if rows=='' then rows=5 /*Not specified? Then use the default.*/
|
||||
mx=rows * (rows+1) % 2 /*calculate maximum value of any value.*/
|
||||
say 'displaying a ' rows " row Floyd's triangle:" /*show header for the triangle.*/
|
||||
say
|
||||
#=1; do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/
|
||||
do #=# for r; i=i+1 /*start to construct a row of triangle.*/
|
||||
_=_ right(#, length(mx-rows+i)) /*build a row of the Floyd's triangle. */
|
||||
end /*#*/
|
||||
say substr(_,2) /*remove 1st leading blank in the line,*/
|
||||
end /*r*/ /* [↑] introduced by first abutment. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
/*REXX pgm displays Floyd's triangle for any number of rows in base 16.*/
|
||||
parse arg rows .; if rows=='' then rows=6 /*Not specified? Use default*/
|
||||
maxV = rows * (rows+1) % 2 /*calculate the max value. */
|
||||
say 'displaying a' rows "row Floyd's triangle in base 16:"; say /*hdr*/
|
||||
#=1; do r=1 for rows; i=0; _= /*row by row.*/
|
||||
do #=# for r; i=i+1 /*start a row*/
|
||||
_ = _ right(d2x(#), length(d2x(maxV - rows + i)))
|
||||
end /*#*/ /* [↑] the triangle row is done.*/
|
||||
say substr(_,2) /*suppress the 1st leading blank,*/
|
||||
end /*r*/ /* [↑] introduced by 1st abutt.*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
/*REXX program constructs & displays Floyd's triangle for any number of rows in base 16.*/
|
||||
parse arg rows .; if rows=='' then rows=6 /*Not specified? Then use the default.*/
|
||||
mx=rows * (rows+1) % 2 /*calculate maximum value of any value.*/
|
||||
say 'displaying a ' rows " row Floyd's triangle in base 16:"; say /*show triangle hdr*/
|
||||
#=1
|
||||
do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/
|
||||
do #=# for r; i=i+1 /*start to construct a row of triangle.*/
|
||||
_=_ right(d2x(#),length(d2x(mx-rows+i))) /*build a row of the Floyd's triangle. */
|
||||
end /*#*/
|
||||
say substr(_,2) /*remove 1st leading blank in the line,*/
|
||||
end /*r*/ /* [↑] introduced by first abutment. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,47 +1,50 @@
|
|||
/*REXX pgm displays Floyd's triangle for any # of rows up to base 90.*/
|
||||
parse arg rows b .; if rows=='' then rows=5 /*Not given? Use default.*/
|
||||
if b=='' then b=10 /*use base 10 if not given.*/
|
||||
maxV = rows * (rows+1) % 2 /*calculate the max value. */
|
||||
say 'displaying a' rows "row Floyd's triangle in base" b':'; say
|
||||
#=1; do r=1 for rows; i=0; _= /*row by row.*/
|
||||
do #=# for r; i=i+1 /*start a row*/
|
||||
_ = _ right(base(#, b), length(base(maxV-rows+i, b)))
|
||||
end /*#*/ /*row is done*/
|
||||
say substr(_,2) /*suppress the 1st leading blank,*/
|
||||
end /*r*/ /* [↑] introduced by 1st abutt.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────BASE subroutine─────────────────────*/
|
||||
base: procedure; parse arg x 1 ox,toB,inB /*get number, toBase, inBase*/
|
||||
@abc='abcdefghijklmnopqrstuvwxyz' /*lowercase Latin alphabet. */
|
||||
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */
|
||||
@@@='0123456789'@abc || @abcU /*prefix 'em with numeric digits.*/
|
||||
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/
|
||||
/*handles up to base 90, special chars must be viewable.*/
|
||||
numeric digits 1000 /*what the hey, support gihugeics*/
|
||||
maxB=length(@@@) /*max base (radix) supported here*/
|
||||
if toB=='' | toB==',' then toB=10 /*if skipped, assume default (10)*/
|
||||
if inB=='' | inB==',' then inB=10 /* " " " " " */
|
||||
if inB<2 | inb>maxB then call erb 'inBase',inB /*bad boy inBase.*/
|
||||
if toB<2 | tob>maxB then call erb 'toBase',toB /* " " toBase.*/
|
||||
if x=='' then call erm /* " " number.*/
|
||||
sigX=left(x,1); if pos(sigX,"-+")\==0 then x=substr(x,2) /*X has sign?*/
|
||||
else sigX= /*no sign. */
|
||||
#=0; do j=1 for length(x) /*convert X, base inB ──► base 10*/
|
||||
_=substr(x, j, 1) /*pick off a numeral from X. */
|
||||
v=pos(_, @@@) /*get the value of this "digit". */
|
||||
if v==0 | v>inB then call erd x,j,inB /*illegal "digit"? */
|
||||
#=#*inB+v-1 /*construct new num, dig by dig. */
|
||||
end /*j*/
|
||||
y=
|
||||
do while #>=toB /*convert #, base 10 ──► base toB*/
|
||||
y=substr(@@@, (#//toB)+1, 1)y /*construct the number for output*/
|
||||
#=#%toB /*··· and whittle # down also. */
|
||||
end /*while #≥toB*/
|
||||
/*REXX program constructs/shows Floyd's triangle for any number of rows in any base ≤90.*/
|
||||
parse arg rows radx . /*obtain optional arguments from the CL*/
|
||||
if rows=='' | rows=="," then rows= 5 /*Not specified? Then use the default.*/
|
||||
if radx=='' | radx=="," then radx=10 /* " " " " " " */
|
||||
mx=rows * (rows+1) % 2 /*calculate maximum value of any value.*/
|
||||
say 'displaying a ' rows " row Floyd's triangle in base" radx':'; say /*display hdr*/
|
||||
#=1
|
||||
do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/
|
||||
do #=# for r; i=i+1 /*start to construct a row of triangle.*/
|
||||
_=_ right(base(#, radx), length(base(mx-rows+i, radx))) /*build triangle row*/
|
||||
end /*#*/
|
||||
say substr(_,2) /*remove 1st leading blank in the line,*/
|
||||
end /*r*/ /* [↑] introduced by first abutment. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
base: procedure; parse arg x 1 ox,toB,inB /*obtain number, toBase, inBase. */
|
||||
@abc='abcdefghijklmnopqrstuvwxyz' /*lowercase Latin alphabet. */
|
||||
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */
|
||||
@@@='0123456789'@abc || @abcU /*prefix 'em with numeric digits.*/
|
||||
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/
|
||||
/*handles up to base 90, all chars must be viewable.*/
|
||||
numeric digits 1000 /*what the hey, support gihugeics*/
|
||||
mxB=length(@@@) /*max base (radix) supported here*/
|
||||
if toB=='' | toB=="," then toB=10 /*if skipped, assume default (10)*/
|
||||
if inB=='' | inB=="," then inB=10 /* " " " " " */
|
||||
if inB<2 | inb>mxB then call erb 'inBase',inB /*invalid/illegal arg: inBase. */
|
||||
if toB<2 | tob>mxB then call erb 'toBase',toB /* " " " toBase. */
|
||||
if x=='' then call erm /* " " " number. */
|
||||
sigX=left(x,1)
|
||||
if pos(sigX,'-+')\==0 then x=substr(x,2) /*X number has a leading sign? */
|
||||
else sigX= /* ··· no leading sign.*/
|
||||
#=0; do j=1 for length(x) /*convert X, base inB ──► base 10*/
|
||||
_=substr(x, j, 1) /*pick off a numeral from X. */
|
||||
v=pos(_, @@@) /*get the value of this "digit". */
|
||||
if v==0 | v>inB then call erd x,j,inB /*is this an illegal "numeral" ? */
|
||||
#=#*inB+v-1 /*construct new num, dig by dig. */
|
||||
end /*j*/
|
||||
y=
|
||||
do while # >= toB /*convert #, base 10 ──► base toB*/
|
||||
y=substr(@@@, (#//toB)+1, 1)y /*construct the number for output*/
|
||||
#=#%toB /* ··· and whittle # down also.*/
|
||||
end /*while*/
|
||||
|
||||
y=sigX || substr(@@@, #+1, 1)y /*prepend the sign if it existed.*/
|
||||
return y /*rturn the number in base toB. */
|
||||
/*──────────────────────────────────error subroutines───────────────────*/
|
||||
erb: call ser 'illegal' arg(2) "base:" arg(1) "must be in range: 2──►" maxB
|
||||
erd: call ser 'illegal "digit" in' x":" _
|
||||
erm: call ser 'no argument specified.'
|
||||
ser: say; say '*** error! ***'; say arg(1); say; exit 13
|
||||
y=sigX || substr(@@@, #+1, 1)y /*prepend the sign if it existed.*/
|
||||
return y /*return the number in base toB.*/
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────*/
|
||||
erb: call ser 'illegal' arg(2) "base:" arg(1) "must be in range: 2──►" mxB
|
||||
erd: call ser 'illegal "digit" in' x":" _
|
||||
erm: call ser 'no argument specified.'
|
||||
ser: say; say '*** error! ***'; say arg(1); say; exit 13
|
||||
|
|
|
|||
|
|
@ -1,9 +1,14 @@
|
|||
input " Number of rows:"; rows
|
||||
html "<table>"
|
||||
for r = 1 to rows ' r = rows
|
||||
html "<TR align=right>"
|
||||
for c = 1 to r ' c = columns
|
||||
j = j + 1
|
||||
html "<TD> ";j;"</TD>"
|
||||
next c
|
||||
next r
|
||||
input "Number of rows: "; rows
|
||||
dim colSize(rows)
|
||||
for col=1 to rows
|
||||
colSize(col) = len(str$(col + rows * (rows-1)/2))
|
||||
next
|
||||
|
||||
thisNum = 1
|
||||
for r = 1 to rows
|
||||
for col = 1 to r
|
||||
print right$( " "+str$(thisNum), colSize(col)); " ";
|
||||
thisNum = thisNum + 1
|
||||
next
|
||||
print
|
||||
next
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
10 LET n=10: LET j=1: LET col=1
|
||||
20 FOR r=1 TO n
|
||||
30 FOR j=j TO j+r-1
|
||||
40 PRINT TAB (col);j;
|
||||
50 LET col=col+3
|
||||
60 NEXT j
|
||||
70 PRINT
|
||||
80 LET col=1
|
||||
90 NEXT r
|
||||
Loading…
Add table
Add a link
Reference in a new issue