Author Topic: Floyd's Triangle  (Read 1790 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2416
    • ScriptBasic Open Source Project
Floyd's Triangle
« on: October 14, 2018, 08:42:11 PM »
Here is the Script BASIC version of the Floyd's Triangle on Rosetta Code.

Code: Script BASIC
  1. ' Floyd's triangle
  2.  
  3. PRINT "Number of rows: "
  4. LINE INPUT rows
  5. rows = VAL(CHOMP(rows))
  6. num = 1
  7. FOR row = 1 TO rows
  8.   FOR col = 1 to row
  9.     PRINT RIGHT("  " & num, LEN(STR(col + rows * (rows - 1) / 2))), " "
  10.     num += 1
  11.   NEXT
  12.   PRINTNL
  13. NEXT
  14.  


jrs@jrs-laptop:~/sb/examples/test$ scriba floyd.sb
Number of rows: 4
1
2 3
4 5 6
7 8 9 10
jrs@jrs-laptop:~/sb/examples/test$ scriba floyd.sb
Number of rows: 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
jrs@jrs-laptop:~/sb/examples/test$ scriba floyd.sb
Number of rows: 20
  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
jrs@jrs-laptop:~/sb/examples/test$



« Last Edit: October 15, 2018, 12:01:13 AM by John »

Offline AlyssonR

  • Advocate
  • Posts: 126
Re: Floyd's Triangle
« Reply #1 on: October 15, 2018, 01:43:00 AM »
Whenever I see this, I wonder who Floyd was, and whether Floyd was just bored to tears in a maths lecture.

Offline John

  • Forum Support / SB Dev
  • Posts: 2416
    • ScriptBasic Open Source Project
Re: Floyd's Triangle
« Reply #2 on: October 15, 2018, 02:17:18 AM »
I can align with that thought.  ;)

I'm surprised the amount of code the other languages used to accomplish the same goal.
« Last Edit: October 15, 2018, 07:16:48 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2416
    • ScriptBasic Open Source Project
Re: Floyd's Triangle
« Reply #3 on: October 16, 2018, 12:59:20 PM »
Quote from: Tomaaz@RetroB
Here is my version of Floyd's Triangle. I wanted to make it as compact as possible and created this one line in Ruby:

Code: Ruby
  1. (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}
  2.  

Anyone wants to post something in BASIC? But please, try to do something different than two loops and couple of variables.  ;)

You're a member here and welcome to create and contribute to existing threads.

Doing multiple statements in one line doesn't mean less code. You have no user input for number of lines which is almost half of the SB code before doing the challenge.
« Last Edit: October 16, 2018, 09:18:15 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2416
    • ScriptBasic Open Source Project
Re: Floyd's Triangle
« Reply #4 on: October 16, 2018, 07:45:06 PM »
Here is the no frils version of Floyd's Triangle in Script BASIC at 101 characters and with user row specification.

Note:  Any INCLUDEs will be counted in the total character count for challenging submissions.

Code: Script BASIC
  1. l=COMMAND
  2. n=1
  3. FOR r=1TO l
  4. FOR c=1TO r
  5. PRINT RIGHT("  "&n,LEN(c+l*(l-1)/2))," "
  6. n+=1
  7. NEXT
  8. PRINTNL
  9. NEXT
  10.  

Notice:
  • No need to convert the command line string argument to a numeric type for use.
  • No need to convert the number to a STR() before concatenation.
  • No need to convert the numeric expression to a STR() to get its LEN().
  • The consensus is there is no need for Script BASIC.


$ time scriba ft.sb 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

real   0m0.006s
user   0m0.006s
sys   0m0.000s
$


Tomaaz's Ruby example totals at 116 characters with a fix number of rows.

Script BASIC wins while remaining highly readable. SB is < 780KB unlike other scripting languages like Ruby in the multi-megabytes. Tomaaz's submission also fails the Rosetta Code requirements due to alignment and enclosing the the results in brackets. Half baked Tomaaz!  :o

Ruby Install

jrs@jrs-laptop:~/sb/examples/test$ sudo apt install ruby
[sudo] password for jrs:
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libatkmm-1.6-1v5 libcairomm-1.0-1v5 libcryptui0a libgtkmm-3.0-1v5 libice6:i386 libjpeg62:i386 libnih-dbus1 libpangomm-1.4-1v5 libsm6:i386 libvte-common libvte9 libxtst6:i386 par2 python-glade2
  python-vte seahorse-daemon
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  fonts-lato libruby2.5 rake ruby-did-you-mean ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit ruby2.5 rubygems-integration
Suggested packages:
  ri ruby-dev bundler
The following NEW packages will be installed:
  fonts-lato libruby2.5 rake ruby ruby-did-you-mean ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit ruby2.5 rubygems-integration
0 upgraded, 11 newly installed, 0 to remove and 23 not upgraded.
Need to get 5,998 kB of archives.
After this operation, 27.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 fonts-lato all 2.0-2 [2,698 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 rubygems-integration all 1.11 [4,994 B]
Get:3 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby2.5 amd64 2.5.1-1ubuntu1 [48.6 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby amd64 1:2.5.1 [5,712 B]
Get:5 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 rake all 12.3.1-1 [45.1 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-did-you-mean all 1.2.0-2 [9,700 B]
Get:7 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-minitest all 5.10.3-1 [38.6 kB]
Get:8 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-net-telnet all 0.1.1-2 [12.6 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-power-assert all 0.3.0-1 [7,952 B]
Get:10 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-test-unit all 3.2.5-1 [61.1 kB]
Get:11 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 libruby2.5 amd64 2.5.1-1ubuntu1 [3,066 kB]
Fetched 5,998 kB in 4s (1,565 kB/s)     
Selecting previously unselected package fonts-lato.
(Reading database ... 269832 files and directories currently installed.)
Preparing to unpack .../00-fonts-lato_2.0-2_all.deb ...
Unpacking fonts-lato (2.0-2) ...
Selecting previously unselected package rubygems-integration.
Preparing to unpack .../01-rubygems-integration_1.11_all.deb ...
Unpacking rubygems-integration (1.11) ...
Selecting previously unselected package ruby2.5.
Preparing to unpack .../02-ruby2.5_2.5.1-1ubuntu1_amd64.deb ...
Unpacking ruby2.5 (2.5.1-1ubuntu1) ...
Selecting previously unselected package ruby.
Preparing to unpack .../03-ruby_1%3a2.5.1_amd64.deb ...
Unpacking ruby (1:2.5.1) ...
Selecting previously unselected package rake.
Preparing to unpack .../04-rake_12.3.1-1_all.deb ...
Unpacking rake (12.3.1-1) ...
Selecting previously unselected package ruby-did-you-mean.
Preparing to unpack .../05-ruby-did-you-mean_1.2.0-2_all.deb ...
Unpacking ruby-did-you-mean (1.2.0-2) ...
Selecting previously unselected package ruby-minitest.
Preparing to unpack .../06-ruby-minitest_5.10.3-1_all.deb ...
Unpacking ruby-minitest (5.10.3-1) ...
Selecting previously unselected package ruby-net-telnet.
Preparing to unpack .../07-ruby-net-telnet_0.1.1-2_all.deb ...
Unpacking ruby-net-telnet (0.1.1-2) ...
Selecting previously unselected package ruby-power-assert.
Preparing to unpack .../08-ruby-power-assert_0.3.0-1_all.deb ...
Unpacking ruby-power-assert (0.3.0-1) ...
Selecting previously unselected package ruby-test-unit.
Preparing to unpack .../09-ruby-test-unit_3.2.5-1_all.deb ...
Unpacking ruby-test-unit (3.2.5-1) ...
Selecting previously unselected package libruby2.5:amd64.
Preparing to unpack .../10-libruby2.5_2.5.1-1ubuntu1_amd64.deb ...
Unpacking libruby2.5:amd64 (2.5.1-1ubuntu1) ...
Setting up fonts-lato (2.0-2) ...
Setting up ruby-did-you-mean (1.2.0-2) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Setting up ruby-net-telnet (0.1.1-2) ...
Setting up rubygems-integration (1.11) ...
Processing triggers for man-db (2.8.3-2) ...
Setting up ruby-minitest (5.10.3-1) ...
Processing triggers for fontconfig (2.12.6-0ubuntu2) ...
Setting up ruby-power-assert (0.3.0-1) ...
Setting up ruby2.5 (2.5.1-1ubuntu1) ...
Setting up ruby (1:2.5.1) ...
Setting up ruby-test-unit (3.2.5-1) ...
Setting up rake (12.3.1-1) ...
Setting up libruby2.5:amd64 (2.5.1-1ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
jrs@jrs-laptop:~/sb/examples/test$


Code: Ruby
  1. (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}
  2.  


jrs@jrs-laptop:~/Ruby$ time ruby ft.rb
[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]

real   0m0.116s
user   0m0.104s
sys   0m0.012s
jrs@jrs-laptop:~/Ruby$


You need a set of Rails to haul that PIG around.
« Last Edit: October 17, 2018, 02:13:40 PM by John »

Offline Mike Lobanovsky

  • ON-HOLD
  • BASIC Developer
  • Posts: 261
Re: Floyd's Triangle
« Reply #5 on: October 17, 2018, 01:29:09 PM »
I'd also note especially that, according to John's output, SB seems to complete the challenge around 20 times faster than that vaunted Ruby thing. ;)

SB underdone? Incomplete? Overestimated? C'mon Tomaaz, you don't really mean that! ;D
Mike
____________________________________________________________________
(3.6GHz Intel Core i5, 16GB RAM / nVidia GTX 1060Ti , 6GB VRAM / x64 Win7 Ult.)

Offline John

  • Forum Support / SB Dev
  • Posts: 2416
    • ScriptBasic Open Source Project
Re: Floyd's Triangle
« Reply #6 on: October 17, 2018, 06:28:40 PM »
I think Tomaaz forgot the golden rule.

Use the right tool for the job.

Offline AlyssonR

  • Advocate
  • Posts: 126
Re: Floyd's Triangle
« Reply #7 on: October 18, 2018, 02:04:28 AM »
When all you have is a hammer, exery problem looks like a nail.

When your toolbox is stuffed to the gunwhales with specialist tools, you tend to use the first tool you put your hand on - even if it is a hammer.

Offline John

  • Forum Support / SB Dev
  • Posts: 2416
    • ScriptBasic Open Source Project
Re: Floyd's Triangle
« Reply #8 on: October 19, 2018, 08:56:03 AM »
Quote from: Mike
SB underdone? Incomplete? Overestimated? C'mon Tomaaz, you don't really mean that!

Taking off those rose colored glasses is hard for some to do.


Offline jalih

  • Advocate
  • Posts: 26
Re: Floyd's Triangle
« Reply #9 on: November 04, 2018, 12:16:41 PM »
Here is my version using 8th programming language:

Code: [Select]
needs stack/rstack

: floyd  \ rows --
  dup >r r@ r@ n:1- 2 n:/ r> n:* n:+ n:int >s s:len nip "%%%dd " s:strfmt >r
  ( >r
    ( r@ n:1- 2 n:/ r@ n:* n:+ n:int 1 rpick s:strfmt . ) 1 r@ loop rdrop cr
  ) 1 rot loop rdrop ;


0 args >n floyd
bye
« Last Edit: November 05, 2018, 08:54:21 AM by jalih »

Offline John

  • Forum Support / SB Dev
  • Posts: 2416
    • ScriptBasic Open Source Project
Re: Floyd's Triangle
« Reply #10 on: November 04, 2018, 12:18:58 PM »
8th grade Floyd.  8)

Outstanding! This should send Tomaaz over the edge.
« Last Edit: November 04, 2018, 12:28:21 PM by John »