41
\$\begingroup\$

Your task is to output a Magical 8 Trapezium:

        1 × 8 + 1 = 9
       12 × 8 + 2 = 98
      123 × 8 + 3 = 987
     1234 × 8 + 4 = 9876
    12345 × 8 + 5 = 98765
   123456 × 8 + 6 = 987654
  1234567 × 8 + 7 = 9876543
 12345678 × 8 + 8 = 98765432
123456789 × 8 + 9 = 987654321
  • Output in your chosen language in the fewest bytes possible.
  • Note the number of spaces at the start of each line to maintain the trapezium shape.
  • Trailing spaces are allowed.
  • You can use × or the letter x - whichever you prefer.
\$\endgroup\$
  • 1
    \$\begingroup\$ Related. (slightly...) \$\endgroup\$ – Martin Ender Jul 25 '16 at 16:04
  • \$\begingroup\$ Middle spaces are required, yes? \$\endgroup\$ – Value Ink Jul 25 '16 at 18:11
  • \$\begingroup\$ @KevinLau-notKenny it is, but you could always post an alternative too if it's significant. \$\endgroup\$ – rybo111 Jul 25 '16 at 18:49
  • \$\begingroup\$ It's 6 bytes corresponding to the 6 spaces in the middle, so no, I don't think it's significant enough. \$\endgroup\$ – Value Ink Jul 25 '16 at 19:23

37 Answers 37

1
2
0
\$\begingroup\$

Pascal, 156 127 103 bytes

Trivial solution, using the Format library func. Shaved some more bytes by using only integers and no strings. Reduced in size thanks to @manatwork (for teaching me some basic stuff about writeln).

var i,c:int64;begin i:=0;for c in[1..9]do begin i:=i*10+c;WriteLn(i:9,' x 8 + ',c,' = ',i*8+c);end;end.

Ungolfed:

var
  i, c: int64;
begin
  i := 0;
  for c in [1..9] do
  begin
    i := i*10+c;
    WriteLn(i:9, ' x 8 + ', c, ' = ', i*8+c);
  end;
end.
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Nice, but why you use Format? WriteLn(i:9,' x 8 + ',c,' = ',i*8+c); And with the price of a compiler warning you can remove i:=0;. (Yes, will not be compatible with some ancient Pascal versions.) \$\endgroup\$ – manatwork Jul 28 '16 at 7:48
  • \$\begingroup\$ @manatwork Because I didn't know about the : operator... Will change it later today \$\endgroup\$ – hdrz Jul 28 '16 at 10:11
  • \$\begingroup\$ I'm afraid, the : is not operator. Is Write/WriteLn specific field width specifier. \$\endgroup\$ – manatwork Jul 28 '16 at 10:31
  • \$\begingroup\$ @manatwork Yeah thats what I meant, didn't know about it. \$\endgroup\$ – hdrz Jul 28 '16 at 11:27
0
\$\begingroup\$

C++, 196 bytes

Ungolfed:

#include <iostream>

using namespace std;

int main()
{
    int i=1;
    while (i<10){
        for (int j=1;j<10-i;j=j+ 1){
            cout << " ";
        }
        for (int j=1;j<=i;j++){
            cout << j;
        }
        cout << " x 8 + 1 = ";

        for(int m=1;m<=i;m++){
                cout << 10 - m;
        }

        cout << endl;
        i = i + 1;
   }
}

Run example:

sh-4.3$ main                                                                                                                                                    
        1 x 8 + 1 = 9                                                                                                                                           
       12 x 8 + 1 = 98                                                                                                                                          
      123 x 8 + 1 = 987                                                                                                                                         
     1234 x 8 + 1 = 9876                                                                                                                                        
    12345 x 8 + 1 = 98765                                                                                                                                       
   123456 x 8 + 1 = 987654                                                                                                                                      
  1234567 x 8 + 1 = 9876543                                                                                                                                     
 12345678 x 8 + 1 = 98765432                                                                                                                                    
123456789 x 8 + 1 = 987654321 
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Hello, and welcome to PPCG! What is the golfed code? \$\endgroup\$ – NoOneIsHere Aug 9 '16 at 17:10
0
\$\begingroup\$

///, 149 bytes

/:/ x 8 + //-/ = //0/1234//_/9876//|/  /||||1:1-9
||| 12:2-98
|||123:3-987
|| 0:4-_
||05:5-_5
| 056:6-_54
|0567:7-_543
 05678:8-_5432
056789:9-_54321

Try it online!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

dc, 79 bytes

1[32P]sS[d0<S1-d0<R]sR[dZ9r-lRxrdn[ x 8 + ]PdZn[ = ]Pdd8*rZ+prd10*dZ+dZA>M]dsMx

Try it online!

Somewhat better than I expected. Started with a solution that juggled stack and an incremental register before realizing I only need to keep the left-most number around - since the value to be added is the number of digits the left-most number has, this can be determined via Z and the result can just be calculated according to the formula.

[32P]sS[d0<S1-d0<R]sR feels wasteful, but I couldn't come up with a much better way to handle the indentation. The main macro, [dZ9r-lRxrdn[ x 8 + ]PdZn[ = ]Pdd8*rZ+prd10*dZ+dZA>M]dsMx should be fairly self-explanatory based on my above description. The initial 1 just seeds the whole thing.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Pyth - 30 bytes

First attempt, working on optimization.

VS9+*dK-9Njd[jkSN\×8\+N\=jkr9K

Try it online.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Forth (gforth), 76 bytes

: f 0 10 1 do cr 10 * i + dup 9 .r ."  x 8 + "i . ." = "dup 8 * i + . loop ;

Try it online!

Explanation

Start a value at 0. Starts a loop from 1 to 9. Each iteration:

  • Multiply value by 10 and add the loop index
  • output this value right-aligned
  • output " x 8 + "
  • output the loop index
  • output " = "
  • calculate results and output

Code Explanation

: f                  \ start word definition
  0                  \ set up counter/value
  10 1 do            \ start a counted loop from 1 to 9
    cr               \ output a newline
    10 * i +         \ calculate the next value for the first term
    dup 9 .r         \ make a copy and then output in a right-aligned space of 9 characters
    ."  x 8 + "      \ output " x 8 +"
    i .              \ output the loop index
    ." = "           \ output " ="
    dup 8 * i +      \ perform the actual calculation
    .                \ output result
  loop               \ end the counted loop
;                    \ end the word definition
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 58 bytes

my $a;printf "%9s x 8 + $_ = %s\n",$a~=$_,$a.flip for 1..9

Try it online!

|improve this answer|||||
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.