# 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.
• Related. (slightly...) Commented Jul 25, 2016 at 16:04
• Middle spaces are required, yes? Commented Jul 25, 2016 at 18:11
• @KevinLau-notKenny it is, but you could always post an alternative too if it's significant. Commented Jul 25, 2016 at 18:49
• It's 6 bytes corresponding to the 6 spaces in the middle, so no, I don't think it's significant enough. Commented Jul 25, 2016 at 19:23

## Python 2, 59 bytes

a=i=1
exec"print'%9d x 8 +'%a,i,'=',a*8+i;i+=1;a=a*10+i;"*9


The numbers a and i the equation a * 8 + i are generated arithmetically. Each line, i is incremented, and a has the next digit appended via a=a*10+i. For example, if a=12345, i=5, then i becomes 6, so the new a is 12345*10 + 6 which is 123456.

Storing these as numbers rather than strings lets us compute the RHS as given by the equation a*8+i, which is shorter than string reversing.

• +1 for seeing this for what it is - a sum that can be generated Commented Jul 25, 2016 at 23:10

# V, 37 bytes

i¸ 1 X 8 + 1 = 98ñYp|Eylp^Xf+$ylp  Try it online! This contains unprintable, so here is a hexdump: 00000000: 69c2 b820 3120 5820 3820 2b20 3120 3d20 i.. 1 X 8 + 1 = 00000010: 391b 38c3 b159 707c 4579 6c70 015e 5866 9.8..Yp|Eylp.^Xf 00000020: 2b01 2479 6c70 18 +.$ylp.


# 05AB1E, 323130 28 bytes

Code:

TG9N-ð×NLJðN"x8+ÿ="€ðJžmN£J,


Uses the CP-1252 encoding. Try it online!.

• .c isn't usable here? Commented Jan 10, 2017 at 14:57
• @carusocomputing It is, but that postdates the challenge. Commented Jan 10, 2017 at 15:01
• Ahhh... Did not see the timestamp. Commented Jan 10, 2017 at 15:10

## PHP, 1058960 57 bytes

my first golf try here (thanks to manatwork & user55641)

for(;$i++<9;)printf("%9s x 8 +$i = %s
",$s.=$i,$s*8+$i);


### 59

for(;$i++<9;)printf("%9s x 8 +$i = %s
",$s.=$i,$t.=10-$i);


### 89 (my own try)

for(;@++$i<=9;){printf("%9s x 8 + %s = %s\n",join(range(1,$i)),$i,join(range(9,10-$i)));}


### 105 (first)

for($j='123456789';@$j[$i++];){printf("%9s x 8 + %s = %s\n",substr($j,0,$i),$i,strrev(substr($j,-$i)));}

• No need for the braces around a single statement. The $i alone better interpolate directly in the string without format specifier. Commented Jul 27, 2016 at 7:16 • You can drop 23 more bytes with a few tricks: Changing @++$i<=9 to $i++<9 saves 2 bytes. You don't need to silence notices as they don't stop execution and under standard PPCG rules you can ignore stderr if you want to. Changing the \n to an actual newline character saves a byte. Changing the join(range(...)) bits to$s.=$i and$t.=10-$i saves 15 bytes. This works because assignments return the value assigned and is pretty much the most valuable trick I've found for golfing php. The last 5 bytes are detailed by manatwork above Commented Jul 27, 2016 at 8:46 • You can drop 2 more bytes by replacing $t.=10-$i with $s*8+$i. tio.run/##K8go@G9jXwAk0/… Commented Mar 8, 2019 at 22:27 • That´s 59 bytes. And $s*8+$i instead of $t.=10-$i saves two more. Commented Mar 9, 2019 at 12:45 # Pyth, 32 bytes VS9ss[*dK-9NSN" x 8 + "N" = "r9K  Try it online! VS9ss[*dK-9NSN" x 8 + "N" = "r9K VS9 # For N in 1..9 s # Join without delimiter s[ # Reduce the array on + (flattens) *dK-9N # - Space, repeated K=(9-N) times SN # - The string sequence 1..N " x 8 + " # - This string literal N # - N itself " = " # - This string literal r9K # - The string sequence 9..K  Thanks to @FryAmTheEggman for saving 2 bytes. Thanks to @KennyLau for saving 3 bytes. • s does not join with space - it joins with no delimiter. Commented Jul 27, 2016 at 4:35 • @isaacg hah, and now I'm thinking I could save a byte by joining with space – Ven Commented Jul 27, 2016 at 9:20 • The byte-count would be the same. Commented Aug 2, 2016 at 2:56 ## CJam, 3938 36 bytes Thanks to Optimizer for saving 2 bytes. 9{)_,:)9Se[" x 8 + "@S'=S9_,fm4$<N}/


Test it here.

Same byte count:

9{)_,:)9Se[]"x8+"+:\'=9_,f-Y$<]S*n}/  This requires the latest version, available on Try it online! • @Optimizer lived up to his name, then! Commented Jul 25, 2016 at 19:02 # Python 2, 878478 75 bytes s="123456789" n=1 exec'print"%9s"%s[:n],"x 8 + %s ="%n,s[::-1][:n];n+=1;'*9  Try it online A previous version uses some string magic. R=range(1,10) for n in R:print" "*(9-n)+R[1:n*3:3]+" x 8 + %d = "%n+R[-2:27-3*n:-3]  Casting range(1,10) to a string gives [1, 2, 3, 4, 5, 6, 7, 8, 9], and this is nice since every number is only a single digit. So getting the string 123456789 from this is simple with range(1,10)[1::3]. The reversed range is  range(1,10)[-2::-3]. Then, to get only as far as I want each iteration, I slice it off at either 3*n, or at 3*(9-n) (27-3*n) for the reversed digits. • You can do for n in range(1,10):print"%9s"%s[:n]+" x 8 + %s = "%n+s[::-1][:n] for 80 bytes. Commented Jul 25, 2016 at 20:02 • s="123456789";n=1;exec'print"%9s"%s[:n],"x 8 + %s ="%n,s[::-1][:n];n+=1;'*9 saves three more! Down to 75. – lynn Commented Jul 25, 2016 at 21:07 • Nice, thanks for the help! Too bad I had to double-slice the second time... Commented Jul 25, 2016 at 21:12 ## Perl, 49 bytes printf"%9s x 8 +$_ = %s
",$@.=$_,$_+8*$@for 1..9


## Batch, 117 bytes

@echo off
set a=         12345678987654321
for /l %%i in (1,1,9)do call echo %%a:~%%i,9%% x 8 + %%i = %%a:~17,%%i%%


Yes, that is 16 % signs on one line; that's Batch for you!

s=(show=<<)
[1..9]>>= \x->([x..8]>>" ")++s[1..x]++" x 8 + "++s[x]++" = "++s[9,8..10-x]++"\n"


How it works:

s=(show=<<)                   -- helper function that turns a list of numbers into
-- a string without delimiters, e.g. [1,2] -> "12"

[1..9]>>=                     -- for each number 1 to 9
([x..8]>>" ")            -- take length of [x..8] copies of a space
s[1..x]                  -- the digits from 1 to x
" x 8 + "                -- a string literal
s[x]                     -- the digit of x
" = "                    -- another string literal
s[9,8..10-x]             -- the digits from 9 down to 10-x
"\n"                     -- an a newline


## Retina, 66 bytes

Byte count assumes ISO 8859-1 encoding. The leading linefeed is significant.


123456789!9 = 987654321
+^((.)+)\B.!.(.+).
$1!$2$3¶$&
!
x 8 +


Try it online!

## Pyke, 30 29 bytes

9Fd*~utj+9<\x8\+9i-\=ji>_dJ)X


Try it here!

9F                         )  -  for i in range(9):
d*                          -       " " * i
+                     -      ^ + V
j                      -       j = V
~ut                       -        "123456789"
9<                   -     ^[:9]
\x8\+9i-\=         -    [^, "x", 8, "+", (9-i), "=", V]
_     -     reversed(V)
ji>      -      j[i:]
dJ   -   " ".join(^)
X - print(reversed(^))


## PowerShell v2+, 85645857 52 bytes

8..0|%{" "*$_+-join(1..++$i+" x 8 + $i = "+9..++$_)}


Loops from 8 to 0 8..0|%{...} via the range operator. Each iteration, we output a string concatenation consisting of (the appropriate number of spaces " "*$_), plus a -joined string of (a range from 1 to a pre-incremented helper number ++$i, plus the middle bit " x 8 + $i = ", plus the final range from 9 to the current number $_ pre-incremented).

One big trick here is we leverage the "left-preference" for typecasting, which allows us to "add" arrays together inside the -join parens, meaning we use only one -join operator.

### Example

PS C:\Tools\Scripts\golfing> .\magical-8-trapezium.ps1
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321

• %{ Are your eyes alright? Commented Jul 25, 2016 at 19:04
• @gcampbell If your eyes looked like that, you'd be frowning, too. Commented Jul 25, 2016 at 19:07
• Depends on how your font renders percents. Commented Jul 25, 2016 at 19:08

# C, 74 bytes

d(i,n){for(i=n=1;i<10;n=++i+n*10)printf("%9d x 8 + %d = %d\n",n,i,n*8+i);}


# MATL, 3836 35 bytes

9:"9@-Z"@:!V' x 8 + '@VO61O58@:-v!D


Try it online!

# J, 51 bytes

(|."1|.\p),.' x 8 + ',"1]p,.' = ',"1]\|.p=:u:49+i.9


Creates the string 123456789 and then operates on prefixes and suffixes of it to create the output.

### Usage

   (|."1|.\p),.' x 8 + ',"1]p,.' = ',"1]\|.p=:u:49+i.9
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321


# JavaScript ES6 (88)

Taking advantage of the new repeat method, backticks and templating...

_=>".........".replace(/./g,(n,i)=>${n=" 123456789".substr(i,9)} x 8 +${++i) = ${n*8+i}\n)  Where \n represents a literal newline character. The second version outputs a trailing newline. I came up with a formula for the numbers ('1'.repeat(9-i)+0+i)/9 but the padding was easier to do this way. # Brainfuck, 232 bytes ++++++++[->+>+>+++++[->+>+>+>+>+>+++<<<<<<]>->>+>++>+++<<<<<<<<]>++>+>>>+++>>>---<<<<<<[-[-<<+>>>>.<<]>+[-<+>>>>+.<<<]>.>>>>>.<<<<<.>>>.<<<.>.<.>>.<<.>>>>.<<<<.<<<<[->>>+>>>+<<<<<<]>>[-<<+>>>>>>.-<<<<]>[->>>-<<<<+>]<<<[->>>+<<<]>.>]  Try it online! Can be golfed much further... ## Javascript (using external library) (143 bytes) n=>_.Range(1,9).WriteLine(v=>_.Range(0,10-v).Write("",x=>" ")+_.Range(1,v).Write("")+" x 8 + " + v + " = "+_.Range(10-v,v).Reverse().Write(""))  Link to lib: https://github.com/mvegh1/Enumerable/ Explanation of code: Create range 1 to 9, and for each value, write a line corresponding to the complex predicate. The predicate is passed the current integer value, and creates a range spanning 10-currentValue elements, in order to create that many spaces. Those spaces are concatenated with the formula part of the line, and then that is concatenated with the tailend of the range matching the number of elements as the frontend, in reverse order. Note: In the image, the first line is off by one space because the console added a quotation mark since the return value is a string. The actual value is formatted correctly # 05AB1E, 24 bytes 9Lε©LJ'x8'+®'=T®L-Jðý}.c  Try it online! Uses a newer version than the challenge, which is now allowed. • It's not much, but in the even newer version of 05AB1E you can remove the ©, and change the ® to y to save a byte. Commented Apr 10, 2019 at 11:29 • @KevinCruijssen Eh, I don't generally "update" old answers like that. Also, the "newer version" is a totally different language (different implementations). Commented Apr 10, 2019 at 18:08 # Canvas, 20 bytes ９｛Ｒ⤢x∙8∙+¹=¹◂±ｍ） ＊］ｒ  Try it here! # VBA (Excel), 51 bytes Using Immediate Window For z=1To 9:a=a &z:?Spc(9-z)a" x 8 +"z"="a*8+z:Next  # k (77 bytes) Could probably be shortened a bit more -1@'((|t)#\:" "),'(t#\:n),'" x 8 + ",/:n,'" = ",/:(t:"I"$'n)#\:|n:"123456789";


Example:

k)-1@'((|t)#\:" "),'(t#\:n),'" x 8 + ",/:n,'" = ",/:(t:"I"$'n)#\:|n:"123456789"; 1 x 8 + 1 = 9 12 x 8 + 2 = 98 123 x 8 + 3 = 987 1234 x 8 + 4 = 9876 12345 x 8 + 5 = 98765 123456 x 8 + 6 = 987654 1234567 x 8 + 7 = 9876543 12345678 x 8 + 8 = 98765432 123456789 x 8 + 9 = 987654321  # golflua, 56 characters p=""~@i=1,9p=p..i; w(S.q("%9s x 8 + %d = "..p*8+i,p,i))$


Sample run:

bash-4.3$golflua -e 'p=""~@i=1,9p=p..i; w(S.q("%9s x 8 + %d = "..p*8+i,p,i))$'
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
`