13
\$\begingroup\$

This question already has an answer here:

The following is the rhombus sequence.

1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321

Your task is to output this, taking no input and using standard output methods (STDOUT, list of numbers or list of strings, etc.).

There's a hitch. You may only use up to one loop - no nested loops. Recursion counts as a loop, so you can either recurse or loop, not both. Non-standard languages will be dealt with on a case-by-case basis.

This is , so shortest program in bytes wins. This is , so you may not take any input whatsoever.

\$\endgroup\$

marked as duplicate by Mego, Shaggy, Wheat Wizard, Peter Taylor code-golf Jul 27 '17 at 19:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ Hey pluto, I edited your challenge to be on-topic here. Let me know if you disagree with anything I said - I think you'll get answers now though, if it gets reopened. \$\endgroup\$ – Stephen Jul 27 '17 at 12:41
  • 3
    \$\begingroup\$ Does mapping over a list count as a loop? \$\endgroup\$ – Business Cat Jul 27 '17 at 13:16
  • 5
    \$\begingroup\$ This is a do X without Y challenge, which are discouraged. \$\endgroup\$ – Mego Jul 27 '17 at 13:18
  • 2
    \$\begingroup\$ FYI: This is , 11², 111², ..., 111111111² \$\endgroup\$ – Engineer Toast Jul 27 '17 at 13:25
  • 4
    \$\begingroup\$ Banning loops is not a source restriction. \$\endgroup\$ – Wheat Wizard Jul 27 '17 at 14:09

22 Answers 22

7
\$\begingroup\$

Mathematica, 23 bytes

((10^#-1)/9)^2&~Array~9

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Nice use of math formulas. I borrowed that for my Python answer too. +1 \$\endgroup\$ – Mr. Xcoder Jul 27 '17 at 13:02
5
\$\begingroup\$

05AB1E, 7 bytes

Only one for-loop here

TG1N×n,

Uses the 05AB1E encoding. Try it online!

Explanation:

TG        # For N in range(1, 10):
  1N×     # Push a string of N 1's
     n    # Square that number
      ,   # Pop and print with a newline
\$\endgroup\$
  • \$\begingroup\$ 9LηεûJ= counts as 1 loop or 3 loops? I guess prefixes would loop and so would range? \$\endgroup\$ – Magic Octopus Urn Jul 27 '17 at 16:31
4
\$\begingroup\$

Python 2, 32 31 bytes

-1 byte thanks to officialaimm

o=0
exec"o=o*10+1;print o*o;"*9

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Jelly, 5 bytes

9ŒḄ€Y

Try it online!

This only uses one mapping loop () since ŒḄ (palindromize) is a builtin already. Explanation:

9ŒḄ€Y
9     9
   €  Map (our looping construct)
 ŒḄ     Palindromize (make range)
    Y Join by newlines.
\$\endgroup\$
  • \$\begingroup\$ ŒḄ€Y everybody, ŒB€Y! \$\endgroup\$ – Erik the Outgolfer Jul 27 '17 at 13:51
2
\$\begingroup\$

Python 2, 42 40 38 35 bytes

-2 thanks to @officialaimm

I ported Jenny_mathy's Mathematica answer to Python, because the arithmetic approach is much shorter:

i=1;exec"print(10**i/9)**2;i+=1;"*9

Try it online!

Exaplanation

  • i=1 - Initializes a variable i to 1.

  • exec"..."*9 - Executes ... 9 times.

  • print(10**i/9)**2 - Prints 10 i and divides the result by 9, which then gets sqaured.

  • i+=1 - Increment i and execute again.


Python 2, 37 bytes

for i in range(9):print(10**-~i/9)**2

Try it online!


Python 2, 63 bytes

This is the initial version.

l='123456789'
for i in l:s=l.index(i);print l[:s]+l[:s+1][::-1]

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ @officialaimm Too late, I already have 35 :) \$\endgroup\$ – Mr. Xcoder Jul 27 '17 at 13:30
1
\$\begingroup\$

SOGL V0.12, 8 7 bytes

9∫Δø∑ΓO

Try it Here!

Explanation:

9∫       do 9 times, pushing 1-indexed counter
  Δ        get 1-indexed (last inclusive) range
   ø∑      join together
     Γ     palindromise
      O    output it
\$\endgroup\$
1
\$\begingroup\$

Dyalog APL, 20 bytes

⍪(' '~⍨∘⍕⍳,1↓⌽∘⍳)¨⍳9

Try it online!

How?

¨⍳9 - for each in range 1..9

    ⍳, - produce the range of this number

    1↓⌽∘⍳ - appended to itself reversed without the last item

     - format to string

    ' '~⍨ - remove spaces

- output vertically

17 bytes, doesn't work because of precision issues

⌽⍕×⍨⍪(10⊥1⍴⍨⊢)¨⍳9

This one uses the i ones squared identity, but replaces the first one on the last line with a 0, because the decoding rounds it up.

\$\endgroup\$
1
\$\begingroup\$

Brachylog, 15 bytes

9⟦₁{⟦₁⟨kc↔⟩cẉ}ᵐ

Try it online!

Yeah this doesn't feel much declarative...

\$\endgroup\$
1
\$\begingroup\$

Vim + coreutils, 42 bytes

i1⏎12<Esc>qqYp$ylp<C-a>q6@qggqqjYpv$!rev⏎kJd2lq7@q

Try it online!

Ungolfed/Explanation

i1⏎12<Esc>                                          " start with lines 1,12 in the buffer
          qq           q6@q                         " record macro and run it 6 times:
            Yp                                      "  - duplicate line
              $ylp                                  "  - duplicate last character
                  <C-a>                             "  - increment it
                           gg                       " go to the beginning (buffer is now: 1,12,123,...,12..89)
                             qq               q7@q  " record macro and run it 6 times:
                               jYp                  "  - go to line below and duplicate it
                                  v$!rev⏎           "  - mark it and reverse it
                                         kJ         "  - join the line above with the current one
                                           d2l      "  - remove the space and character that are too much

vim gif

\$\endgroup\$
0
\$\begingroup\$

Haskell, 39 32 bytes

No for loops/recursion at all, makes use of list comprehension:

[[1..x]++[x-1,x-2..1]|x<-[1..9]]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ List of numbers is allowed output, however im not sure if list of list of numbers is allowed. \$\endgroup\$ – Rames Jul 27 '17 at 15:59
0
\$\begingroup\$

Scala, 69 bytes

There is only one "loop", when I iterate over (1 to 9). But you could consider that there are loops in reverse and Range instanciations.

(1 to 9).map(x=>println(((1 to x)++(1 to x-1).reverse).mkString("")))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

AHK, 55 bytes

a=1
Loop,9{ 
b:=a**2
Send %b%`n
a:=a 1
}
Send {VK08 2}1

This was too stupid of a result for me to not post. AHK messes rounds the last output to 12345678987654320 so we backspace twice and add a 1.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 11 bytes

jm^s*d\12S9

Try it online!


How does this work?

 m       S9         - Map over [1...9]
  ^s    2           - The square of the integer formed by:
    *d\1            - the string with n consecutive 1s.
j                   - Join by newlines
\$\endgroup\$
0
\$\begingroup\$

Google Sheets, 66 bytes

=ArrayFormula(Join("
",REPT(1,ROW(A1:A8))^2,"12345678987654321"))

No loops, just a matrix. It would only be 46 bytes but Sheets only has 15 decimal points of precision so the last number is displayed as 12345678987654300 or 1.23457E+1621 so I had to add it manually.

Result

\$\endgroup\$
0
\$\begingroup\$

R, 18 bytes

cumsum(10^(0:8))^2

Returns the values.

Try it online!

Due to floating point precision issues, the last number is 12345678987654320 which is equal to 12345678987654321 in R, as the footer shows. In the header, it sets the options to get the numbers to print in non-scientific notation.

\$\endgroup\$
0
\$\begingroup\$

C#, 101 bytes

using System.Linq;_=>new int[9].Select((n,i)=>"123456789".Substring(0,i+1)+"87654321".Substring(8-i))

Try it online!


Or the loop approach for 111 bytes:

_=>{var a=new string[9];for(int i=0;i<9;)a[i]="123456789".Substring(0,++i)+"87654321".Substring(9-i);return a;}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Kotlin, 46 bytes

(1..9).map{"1".repeat(it).toLong()}.map{it*it}
\$\endgroup\$
0
\$\begingroup\$

QBIC, 17 bytes

[9|?((z^a-1)/9)^2

From oeis.

\$\endgroup\$
0
\$\begingroup\$

Swift, 76 bytes

import Foundation
for i in 1...9{print(Int(pow((pow(10,Double(i))-1)/9,2)))}

Try it online!

If printing as an array of lines is allowed, here is 74 byte approach:

import Foundation
print((1...9).map{Int(pow((pow(10,Double($0))-1)/9,2))})

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 22 bytes

say((1x$_)**2)for 1..9

Try it online!

Using Jenny_mathy's formula to save a lot of bytes.

\$\endgroup\$
0
\$\begingroup\$

PHP, 38 37 bytes

for(;$i<9**8;)echo(($i.='1')**2)."
";

with warnings

PHP, 42 41 bytes

for($i=0;$i<9**8;)echo(($i.='1')**2)."
";

without warnings

\$\endgroup\$
0
\$\begingroup\$

Javascript 66 bytes

Returns an array of strings representing the numbers in the sequence (necessary because 12345678987654321 is too large to be accurately represented as an int in Javascript)

o=>Array(9).fill``.map(x=>(x+(n=n*10+1)*n).replace(0,1),n=‌​0)

Explanation

o=>                               // o is unused input (saves 1 byte vs ())
    Array(9)                      // create an array of length 9
        .fill``                   // fill it with empty strings
                                  // we must fill it with something, because map skips
                                  // empty entries
        .map(                     // loop over the array and return a new array with the
                                  // values returned by the callback
            x=>(                  // x is the element in the array
                (x+(n=n*10+1)*n)  // make n 1, 11, 111, 1111, etc, and get the string
                                  // value of n^2
                    .replace(0,1) // replace 0's with 1's (necessary for
                                  // 12345678987654321, because
                                  // 111111111^2 = 12345678987654320 in javascript)
            ),
            n=0                   // n^2 is the rhombus number at each stage
        )
\$\endgroup\$
  • \$\begingroup\$ -6 bytes: o=>Array(9).fillBTBT.map(x=>(x+(n=n*10+1)*n).replace(0,1),n=0) (replace BT with backticks) \$\endgroup\$ – Herman L Jul 28 '17 at 6:17
  • \$\begingroup\$ Thanks, I should have remember the backticks bit. Apparently my golf game just wasn't on yesterday >;o) \$\endgroup\$ – asgallant Jul 28 '17 at 14:50

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