Output Rhombus Sequence without Nested Loops

13

1

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.

pluto20010

Posted 2017-07-27T11:35:35.837

Reputation: 247

Question was closed 2017-07-27T19:53:10.260

1Hey 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. – Stephen – 2017-07-27T12:41:26.950

Appreciate the effort @StepHen! I think I should have put my question this way itself. – pluto20010 – 2017-07-27T12:51:34.343

3Does mapping over a list count as a loop? – Business Cat – 2017-07-27T13:16:05.867

5

This is a do X without Y challenge, which are discouraged.

– Mego – 2017-07-27T13:18:14.163

2FYI: This is , 11², 111², ..., 111111111² – Engineer Toast – 2017-07-27T13:25:23.350

1@Shaggy I don't think so, this is [tag:restricted-source]. – Erik the Outgolfer – 2017-07-27T13:36:02.413

4Banning loops is not a source restriction. – Post Rock Garf Hunter – 2017-07-27T14:09:45.640

Is looping & recursion only banned at the highest level (explicitly instructed in the source) or should lower levels avoid them too; if so to what level? – Jonathan Allan – 2017-07-27T19:14:34.677

Answers

7

Mathematica, 23 bytes

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

Try it online!

J42161217

Posted 2017-07-27T11:35:35.837

Reputation: 15 931

1Nice use of math formulas. I borrowed that for my Python answer too. +1 – Mr. Xcoder – 2017-07-27T13:02:05.693

5

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

Adnan

Posted 2017-07-27T11:35:35.837

Reputation: 41 965

9LηεûJ= counts as 1 loop or 3 loops? I guess prefixes would loop and so would range? – Magic Octopus Urn – 2017-07-27T16:31:25.657

4

Python 2, 32 31 bytes

-1 byte thanks to officialaimm

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

Try it online!

Rod

Posted 2017-07-27T11:35:35.837

Reputation: 17 588

2

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!

Mr. Xcoder

Posted 2017-07-27T11:35:35.837

Reputation: 39 774

1@officialaimm Too late, I already have 35 :) – Mr. Xcoder – 2017-07-27T13:30:36.803

2

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.

Erik the Outgolfer

Posted 2017-07-27T11:35:35.837

Reputation: 38 134

ŒḄ€Y everybody, ŒB€Y! – Erik the Outgolfer – 2017-07-27T13:51:40.360

1

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

dzaima

Posted 2017-07-27T11:35:35.837

Reputation: 19 048

1

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.

Uriel

Posted 2017-07-27T11:35:35.837

Reputation: 11 708

1

Brachylog, 15 bytes

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

Try it online!

Yeah this doesn't feel much declarative...

Erik the Outgolfer

Posted 2017-07-27T11:35:35.837

Reputation: 38 134

1

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

ბიმო

Posted 2017-07-27T11:35:35.837

Reputation: 15 345

0

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

Mr. Xcoder

Posted 2017-07-27T11:35:35.837

Reputation: 39 774

0

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!

ბიმო

Posted 2017-07-27T11:35:35.837

Reputation: 15 345

List of numbers is allowed output, however im not sure if list of list of numbers is allowed. – Rames – 2017-07-27T15:59:34.130

0

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!

V. Courtois

Posted 2017-07-27T11:35:35.837

Reputation: 868

0

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

Engineer Toast

Posted 2017-07-27T11:35:35.837

Reputation: 5 769

0

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.

Engineer Toast

Posted 2017-07-27T11:35:35.837

Reputation: 5 769

0

QBIC, 17 bytes

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

From oeis.

steenbergh

Posted 2017-07-27T11:35:35.837

Reputation: 7 772

0

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.

Giuseppe

Posted 2017-07-27T11:35:35.837

Reputation: 21 077

0

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!

TheLethalCoder

Posted 2017-07-27T11:35:35.837

Reputation: 6 930

0

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
        )

asgallant

Posted 2017-07-27T11:35:35.837

Reputation: 309

-6 bytes: o=>Array(9).fillBTBT.map(x=>(x+(n=n*10+1)*n).replace(0,1),n=0) (replace BT with backticks) – Herman L – 2017-07-28T06:17:51.327

Thanks, I should have remember the backticks bit. Apparently my golf game just wasn't on yesterday >;o) – asgallant – 2017-07-28T14:50:24.097

0

Kotlin, 46 bytes

(1..9).map{"1".repeat(it).toLong()}.map{it*it}

Rames

Posted 2017-07-27T11:35:35.837

Reputation: 221

0

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

jstnthms

Posted 2017-07-27T11:35:35.837

Reputation: 181

0

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!

Mr. Xcoder

Posted 2017-07-27T11:35:35.837

Reputation: 39 774

0

Perl 5, 22 bytes

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

Try it online!

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

Xcali

Posted 2017-07-27T11:35:35.837

Reputation: 7 671