Output a Magical 8 Trapezium

41

3

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.

rybo111

Posted 2016-07-25T16:01:19.207

Reputation: 4 071

1Related. (slightly...) – Martin Ender – 2016-07-25T16:04:30.780

Middle spaces are required, yes? – Value Ink – 2016-07-25T18:11:38.653

@KevinLau-notKenny it is, but you could always post an alternative too if it's significant. – rybo111 – 2016-07-25T18:49:58.080

It's 6 bytes corresponding to the 6 spaces in the middle, so no, I don't think it's significant enough. – Value Ink – 2016-07-25T19:23:23.410

Answers

15

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.

xnor

Posted 2016-07-25T16:01:19.207

Reputation: 115 687

+1 for seeing this for what it is - a sum that can be generated – rybo111 – 2016-07-25T23:10:45.900

7

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.

James

Posted 2016-07-25T16:01:19.207

Reputation: 54 537

5

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.

Ven

Posted 2016-07-25T16:01:19.207

Reputation: 3 382

s does not join with space - it joins with no delimiter. – isaacg – 2016-07-27T04:35:24.897

@isaacg hah, and now I'm thinking I could save a byte by joining with space – Ven – 2016-07-27T09:20:52.807

The byte-count would be the same.

– Leaky Nun – 2016-08-02T02:56:31.210

5

05AB1E, 32 31 30 28 bytes

Code:

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

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

Adnan

Posted 2016-07-25T16:01:19.207

Reputation: 41 965

.c isn't usable here? – Magic Octopus Urn – 2017-01-10T14:57:38.850

@carusocomputing It is, but that postdates the challenge. – Adnan – 2017-01-10T15:01:32.693

Ahhh... Did not see the timestamp. – Magic Octopus Urn – 2017-01-10T15:10:20.353

5

PHP, 105 89 60 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)));}

Crypto

Posted 2016-07-25T16:01:19.207

Reputation: 862

1No need for the braces around a single statement. The $i alone better interpolate directly in the string without format specifier. – manatwork – 2016-07-27T07:16:45.357

1You 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 – user55641 – 2016-07-27T08:46:00.490

1

You can drop 2 more bytes by replacing $t.=10-$i with $s*8+$i. https://tio.run/##K8go@G9jXwAk0/KLNKxVMrW1bSytNQuKMvNK0jSUVC2LFSoULBS0FVQyFWwVVIu5lHRUivVsVTKBlJaFtkqmpvX//wA

– 640KB – 2019-03-08T22:27:43.870

1That´s 59 bytes. And $s*8+$i instead of $t.=10-$i saves two more. – Titus – 2019-03-09T12:45:21.470

4

CJam, 39 38 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!

Martin Ender

Posted 2016-07-25T16:01:19.207

Reputation: 184 808

9@Optimizer lived up to his name, then! – rybo111 – 2016-07-25T19:02:36.767

4

Python 2, 87 84 78 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.

mbomb007

Posted 2016-07-25T16:01:19.207

Reputation: 21 944

You can do for n in range(1,10):print"%9s"%s[:n]+" x 8 + %s = "%n+s[::-1][:n] for 80 bytes. – TheBikingViking – 2016-07-25T20:02:15.970

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 – 2016-07-25T21:07:07.270

Nice, thanks for the help! Too bad I had to double-slice the second time... – mbomb007 – 2016-07-25T21:12:07.647

4

Ruby, 77 73 65 60 bytes

Try it online~

Major revamps from @manatwork

Another overhaul from @xsot

a=i=0;9.times{puts"%9d x 8 + %d = %d"%[a=a*10+i+=1,i,a*8+i]}

Value Ink

Posted 2016-07-25T16:01:19.207

Reputation: 10 608

Seems to be shorter with format string: puts'%9d x 8 + %d = %d'%[k=[*1..i]*'',i,k.to_i*8+i]. – manatwork – 2016-07-26T09:10:11.780

(1..9).map1.upto(9) – manatwork – 2016-07-26T09:25:11.693

Ah, I didn't know about %9d being a formatting option to pad integers like that – Value Ink – 2016-07-26T18:23:47.957

60: a=i=0;9.times{puts"%9d x 8 + %d = %d"%[a=a*10+i+=1,i,a*8+i]} – xsot – 2016-07-27T10:48:01.447

@xsot that's great! Didn't think of calculating the initial number like that. – Value Ink – 2016-07-28T08:15:47.567

4

Java 10, 151 133 130 129 126 110 bytes

v->{String p="\n",r="";for(int n=123456789,i=9;i>0;n/=10,p+=" ")r=p+n+" x 8 + "+i+" = "+(n*8+i--)+r;return r;}

Try it online.

Explanation:

v->{                   // Method with empty unused parameter and String return-type
  String p="\n",       //  Prefix-String, starting at a newline
         r="";         //  Result-String, starting empty
  for(int n=123456789, //  Multiply-number, starting at 123456789
      i=9;i>0          //  Loop `i` in the range [9, 0):
      ;                //    After every iteration:
       n/=10,          //     Remove the last digit from the integer
       p+=" ")         //     Append a space after the prefix
    r=...+r;           //   Prepend the following to the result-String:
      p                //    The prefix-String
      +n               //    Followed by the integer
      +" x 8 + "       //    Followed by the literal String " x 8 + "
      +i               //    Followed by the loop-index `i`
      +" = "           //    Followed by the literal String " = "
      +(n*8+i--)       //    Followed by the result of that equation
  return r;}           //  Return the result-String

Kevin Cruijssen

Posted 2016-07-25T16:01:19.207

Reputation: 67 575

1I think you could save bytes by using x instead of the multiplication sign. – wizzwizz4 – 2016-07-26T09:49:10.273

1You can save a couple of bytes by initializing s to "\n" and removing "\n"+ from the for loop – cliffroot – 2016-07-26T09:59:46.320

@wizzwizz4 Thanks. Should have known × is 2 bytes instead of 1 like x.. – Kevin Cruijssen – 2016-07-26T12:00:00.313

Aren't you adding s to the result on each iteration as well? – cliffroot – 2016-07-26T12:13:35.217

I know this is old, but can't you do return o instead of System.out.print(o)? Also, you can change to Java 10 and save with var and lambdas – Embodiment of Ignorance – 2019-03-08T19:06:35.410

@EmbodimentofIgnorance Done. This is indeed one of my earliest answers. :) Not even sure why I had the print instead of return.. xD PS: I cannot use var since there are two Strings. I could use something like var p="\n";var r="";, but that would be 1 byte longer instead of shorter. – Kevin Cruijssen – 2019-03-09T12:08:07.947

4

Perl, 49 bytes

printf"%9s x 8 + $_ = %s
",$@.=$_,$_+8*$@for 1..9

Usage

perl -e 'printf"%9s x 8 + $_ = %s
",$@.=$_,$_+8*$@for 1..9'

Dom Hastings

Posted 2016-07-25T16:01:19.207

Reputation: 16 415

3

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);}

anatolyg

Posted 2016-07-25T16:01:19.207

Reputation: 10 719

3

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!

Neil

Posted 2016-07-25T16:01:19.207

Reputation: 95 035

3

C#, 113 bytes

void f(){for(int n=1,i=1;i<10;n=10*n+ ++i)Console.WriteLine(new string(' ',9-i)+n+" x "+"8 + "+i+" = "+(n*8+i));}

if you have anyway to improve this solution feel free to share.

ScifiDeath

Posted 2016-07-25T16:01:19.207

Reputation: 151

You can save 1 byte by removing a space: ;n=10*n+ ++i in the for-loop can be changed to ;n=++i+10*n. Also, +" x "+"8 + "+ can be changed to +" x 8 + "+. to save 3 more bytes. – Kevin Cruijssen – 2016-07-27T06:50:01.653

void f(){for(int n=1,i=1;i<10;n=++i+10n)Console.WriteLine($"{new string(' ', 9-i)}{n} x 8 + {i} = {(n8+i)}");}

------------ saved you a byte! – downrep_nation – 2016-07-27T09:26:22.927

2

APL (Dyalog Unicode), 61 52 39 bytesSBCS

↑(⍳9)((¯9↑↑),' x 8 +',⊣,'= ',↑∘⌽)¨⊂1↓⎕D

Try it online!

-9 bytes by using the 10⊥ trick to parse the number, instead of a reduction. Thanks to @Adám for -13!

Explanation:

↑    ((¯9↑↑),' x 8 +',⊣,'= ',↑∘⌽)¨⊂1↓⎕D
                                     ⎕D  ⍝ Numbers from 0 to 9
                                   1↓    ⍝ Drop the 0
 (⍳9)(                          )¨⊂      ⍝ Do 9 times, N=current
                             ↑∘⌽         ⍝ Reverse the string (9..1) and cut off N elements
                      ⊣                  ⍝ N itself
      (   ↑)                             ⍝ Drop N elements off the 1..9 string...
      (¯9↑ )                             ⍝ ...then pad it back with spaces
            ,' x 8 +', ,'= ',            ⍝ Join with a few constant strings
↑                                        ⍝ Format

Ven

Posted 2016-07-25T16:01:19.207

Reputation: 3 382

2

PowerShell v2+, 85 64 58 57 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

AdmBorkBork

Posted 2016-07-25T16:01:19.207

Reputation: 41 581

4%{ Are your eyes alright? – gcampbell – 2016-07-25T19:04:36.833

@gcampbell If your eyes looked like that, you'd be frowning, too. – AdmBorkBork – 2016-07-25T19:07:10.067

Depends on how your font renders percents. – gcampbell – 2016-07-25T19:08:52.537

2

Haskell, 92 bytes

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

nimi

Posted 2016-07-25T16:01:19.207

Reputation: 34 639

2

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(^))

Blue

Posted 2016-07-25T16:01:19.207

Reputation: 26 661

2

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!

Martin Ender

Posted 2016-07-25T16:01:19.207

Reputation: 184 808

2

MATL, 38 36 35 bytes

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

Try it online!

Luis Mendo

Posted 2016-07-25T16:01:19.207

Reputation: 87 464

2

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

miles

Posted 2016-07-25T16:01:19.207

Reputation: 15 654

2

JavaScript ES6 (88)

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

i=10;for(y="";--i;)console.log(`${" ".repeat(i)+(y+=(x=10-i))} x 8 + ${x} = ${y*8+x}\n`)

WallyWest

Posted 2016-07-25T16:01:19.207

Reputation: 6 949

nice job bro , you should consider to remove some space and use alert instead of console.log, it can save some bytes! – chau giang – 2019-04-14T12:57:32.410

Given I answered this just before midnight I figure I was close to half asleep... I'll post an update on this soon. LOL – WallyWest – 2019-04-14T20:16:29.993

2

R, 107 103 bytes

a=1;for(i in 2:10){cat(rep("",11-i),paste(a,"x",8,"+",(i-1),"=",strtoi(a)*8+(i-1)),"\n");a=paste0(a,i)}

Ungolfed :

a=1

for(i in 2:10)
    cat(rep("",11-i),paste(a,"x",8,"+",(i-1),"=",strtoi(a)*8+(i-1)),"\n")
    a=paste0(a,i)

Result :

        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

Frédéric

Posted 2016-07-25T16:01:19.207

Reputation: 2 059

96 bytes – Giuseppe – 2019-03-08T16:24:00.130

1

05AB1E, 24 bytes

9Lε©LJ'x8'+®'=T®L-Jðý}.c

Try it online!

Uses a newer version than the challenge, which is now allowed.

Erik the Outgolfer

Posted 2016-07-25T16:01:19.207

Reputation: 38 134

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. – Kevin Cruijssen – 2019-04-10T11:29:09.360

@KevinCruijssen Eh, I don't generally "update" old answers like that. Also, the "newer version" is a totally different language (different implementations). – Erik the Outgolfer – 2019-04-10T18:08:32.070

1

Canvas, 20 bytes

9{R⤢x∙8∙+¹=¹◂±m) *]r

Try it here!

dzaima

Posted 2016-07-25T16:01:19.207

Reputation: 19 048

1

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

remoel

Posted 2016-07-25T16:01:19.207

Reputation: 511

1

JavaScript (ES6), 99 bytes

_=>[...Array(9)].map((n,i)=>`${n="        123456789".substr(i,9)} x 8 + ${++i} = ${n*8+i}`).join`\n`
_=>".........".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.

Neil

Posted 2016-07-25T16:01:19.207

Reputation: 95 035

1

Brainfuck, 232 bytes

++++++++[->+>+>+++++[->+>+>+>+>+>+++<<<<<<]>->>+>++>+++<<<<<<<<]>++>+>>>+++>>>---<<<<<<[-[-<<+>>>>.<<]>+[-<+>>>>+.<<<]>.>>>>>.<<<<<.>>>.<<<.>.<.>>.<<.>>>>.<<<<.<<<<[->>>+>>>+<<<<<<]>>[-<<+>>>>>>.-<<<<]>[->>>-<<<<+>]<<<[->>>+<<<]>.>]

Try it online!

Can be golfed much further...

Leaky Nun

Posted 2016-07-25T16:01:19.207

Reputation: 45 011

1

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

enter image description here

applejacks01

Posted 2016-07-25T16:01:19.207

Reputation: 989

0

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.

brhfl

Posted 2016-07-25T16:01:19.207

Reputation: 1 291

0

Pyth - 30 bytes

First attempt, working on optimization.

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

Try it online.

Maltysen

Posted 2016-07-25T16:01:19.207

Reputation: 25 023

0

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

reffu

Posted 2016-07-25T16:01:19.207

Reputation: 1 361

0

Perl 6, 58 bytes

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

Try it online!

bb94

Posted 2016-07-25T16:01:19.207

Reputation: 1 831

0

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

skeevey

Posted 2016-07-25T16:01:19.207

Reputation: 4 139

0

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

manatwork

Posted 2016-07-25T16:01:19.207

Reputation: 17 865

0

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.

hdrz

Posted 2016-07-25T16:01:19.207

Reputation: 321

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.) – manatwork – 2016-07-28T07:48:10.987

@manatwork Because I didn't know about the : operator... Will change it later today – hdrz – 2016-07-28T10:11:58.413

I'm afraid, the : is not operator. Is Write/WriteLn specific field width specifier. – manatwork – 2016-07-28T10:31:45.840

@manatwork Yeah thats what I meant, didn't know about it. – hdrz – 2016-07-28T11:27:24.553

0

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 

darkness404

Posted 2016-07-25T16:01:19.207

Reputation: 1

Hello, and welcome to PPCG! What is the golfed code? – NoOneIsHere – 2016-08-09T17:10:29.253

0

///, 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!

Erik the Outgolfer

Posted 2016-07-25T16:01:19.207

Reputation: 38 134