RetroBASIC
Basicprogramming(.org) => Code and examples => Topic started by: Tomaaz on October 16, 2018, 07:31:11 PM
-
Here is my version of Floyd's Triangle (http://rosettacode.org/wiki/Floyd%27s_triangle). I wanted to make it as compact as possible and created this one line in Ruby:
(1..14).each {|x| puts (((1 + x) * (x / 2.0) - (x - 1)).to_i..((1 + x) * (x / 2.0)).to_i).to_a.to_s.split(",").join}
Anyone wants to post something in BASIC? But please, try to do something different than two loops and couple of variables. ;)
-
Here's what I have:
-
SmallBASIC version:
floyd 20
sub floyd (r)
locate r, 1
for i in seq(((r - 1) * r) / 2 + 1, r * (r + 1) / 2, r * (r + 1) / 2 - (r - 1) * r / 2 ) do ? right(" " + str(i), 4);
? : if r > 0 then floyd (r - 1) else locate 1, 4 : ? 1
end
-
Recursion. Nice. Anyone else? ;)
-
Dang! I missed the Rosetta spec about spacing, that probably eliminates a recursive method or makes it very ugly.
-
Yep! John's got a good one! :)
Works in JB too:
' "Floyd John's version" ' from All BASIC 2018-10-17 looking pretty good!"
l = 14 'number of rows for Floyd to process
n = 1 ' number increment
FOR r = 1 TO l
FOR c = 1 TO r
PRINT RIGHT$(" " + STR$(n), LEN(STR$(c + l * (l - 1) / 2)) + 1);
n = n + 1
NEXT
PRINT
NEXT
Output: 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
-
Yep! John's got a good one! :)
Yes Mark,
And while we're at it let me note that John's code beats (https://www.allbasic.info/forum/index.php?topic=510.msg5487#msg5487) Tomaaz's kabbala hands down 3 times:- readability wise
- code length wise
- speed wise
Slightly underdone, you say? ;)
;D
-
Version 1, a smooth triangle (but not exactly what the Rosetta specs ask for):
include \masm32\MasmBasic\MasmBasic.inc ; download (http://masm32.com/board/index.php?topic=94.0)
SetGlobals rows, columns, ct
Init
For_ rows=0 To 19
For_ columns=0 To rows
inc ct
Print Str$("%__i ", ct)
Next
Print
Next
Inkey
EndOfCode
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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
Version 2, strictly following the spacing specs: (https://rosettacode.org/wiki/Floyd%27s_triangle)
include \masm32\MasmBasic\MasmBasic.inc ; download (http://masm32.com/board/index.php?topic=94.0)
SetGlobals rows, columns, ct
Init
round=0
repeat 2
Clr ct
For_ rows=0 To 4+9*round
For_ columns=0 To rows
inc ct
Print Str$("%__i", ct)
.if columns>6
Print " "
.endif
Next
Print
Next
Print
round=round+1
endm
Inkey
EndOfCode
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
-
Yep! John's got a good one! :)
Yes Mark,
And while we're at it let me note that John's code beats (https://www.allbasic.info/forum/index.php?topic=510.msg5487#msg5487) Tomaaz's kabbala hands down 3 times:- readability wise
- code length wise
- speed wise
Slightly underdone, you say? ;)
;D
While I really enjoyed reading your comments, I need to say that you've totally missed the point of my example. :) It wasn't about speed, readability was the last thing I was concerned about and I didn't really care about the beauty of the output. It was more about finding a less obvious mathematical solution to the problem. Also I wanted it to be specific to the used language. Of course, I could have written something like this:
x, y = 1, 1
print "Number of rows? "
gets.to_i.times do
y.times do
print x, "\t"
x += 1
end
puts
y += 1
end
The question is - is this boring and obvious code worth posting? IMO, it isn't. Hence "But please, try to do something different than two loops and couple of variables. ;)" at the end of my post. This code can be easily translated to almost every language. The one from my first post can't, because, due to use of ranges, arrays and OO syntax is Ruby specific. Also, I wanted to have one variable in the entire program. It took me about half an hour to re-study high school level maths, but it was fun.
-
Tomaaz,
... due to use of ranges, arrays and OO syntax is Ruby specific.
What was the point in building your challenge around alien language specific features? What were you expecting from the Beginner's ASIC audience? Lists, ranges, and OOP re-written via line numbers and goto's???
There's no such stuff in BASIC and will never be, and you know that no worse than I do regardless of any meaning you pretend to have put in your challenge.
There's absolutely no point either in an alternative mathematical solution that's almost twice longer, twenty times slower, and cryptic beyond recognition at least to the BASIC audience. IMO you plain wasted that half hour of your life for nothing.
-
It wasn't about speed, readability was the last thing I was concerned about and I didn't really care about the beauty of the output. ... but it was fun.
You should add it to the Rosetta code Ruby example (https://rosettacode.org/wiki/Floyd%27s_triangle#Ruby). Your coding style is certainly different from mine, but when I saw your one-liner, my first thought was WOW! ;)
@Mike: No, that was no waste of time. Tomaaz impressed you and me, and he had fun.
-
well tommat ( ;D ) like to to do such a stuff ,,,yee haaa
here is one in REXX
This REXX version uses a simple formula to calculate the maximum value (triangle element) displayed.
/*REXX program constructs & displays Floyd's triangle for any number of specified rows.*/
parse arg N .; if N=='' | N=="," then N= 5 /*Not specified? Then use the default.*/
mx= N * (N+1) % 2 - N /*calculate the maximum of any value. */
say 'displaying a ' N " row Floyd's triangle:" /*show the header for Floyd's triangle.*/
say /*display a blank line below the title.*/
#=1; do r=1 for N; 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+i ) ) /*build a row of the Floyd's triangle. */
end /*#*/ /*calculate the max length on the fly. */
say substr(_, 2) /*remove 1st leading blank in the line.*/
end /*r*/ /*stick a fork in it, we're all done. */
-
Mike, you wasted much more than 30 min. for talking about me wasting 30 min. That's rather funny. ;D What is not funny is your patronising style and constan attempts to convert every single topic to BASIC vs other languages competition. I want to show and see differences between languages. I'm not interested in finding overall winner, because it's simply impossible.
-
I have to say, what Tomaaz contributes to this forum has usually been challenging, interesting, stimulating, at least to this BASIC fan.
I also appreciate Mike's (no L needed) experienced tech point of view that can see through smoke and mirrors eg the illusion of one-liners.
What is boring is hostility or hysterics ;) which I think Mike himself has noted when I screwed up and Tomaaz has mentioned a time or two too.
And I have to agree with JJ on two points:
1. It is NOT a waste of time when someone is having fun at no other's expense.
2. The fixed spacing, right alignment of columns in Floyd's Triangle just plain looks better with a nice crisp line down the slope.