33
\$\begingroup\$

This idea came to me when I saw my little brother playing with my calculator :D

The task

Taking a integer as an input, print that many graphical square roots under each other, like this:

n = 1

  ___
\/ 1


n = 3
    _______
   / _____
  / / ___
\/\/\/ 3


n = 5

      ___________
     / _________
    / / _______
   / / / _____
  / / / / ___
\/\/\/\/\/ 5


n = 10

           ______________________
          / ____________________
         / / __________________
        / / / ________________
       / / / / ______________
      / / / / / ____________
     / / / / / / __________
    / / / / / / / ________
   / / / / / / / / ______
  / / / / / / / / / ____
\/\/\/\/\/\/\/\/\/\/ 10

Each root consists of 4 parts, which I'm going to very scientifically name:

(s is the root size on the stack of roots, n is the input number, x is the number of digits)

  1. The "tail", which is a single \
  2. The "wall", which consists of / * s
  3. The "roof", which consists of _ * 2 * s + x
  4. And the number n under the smallest root, placed in the centre (leaving one empty space under the last _ in the smallest root)

Input

You must take input of the number n, no hardcoding the n

Output

The ascii roots your program made

This is a code-golf challenge, so lowest byte count for each language wins!

\$\endgroup\$
12
  • 3
    \$\begingroup\$ Will the number exceed the digit 9? \$\endgroup\$
    – user92069
    Commented May 9, 2020 at 8:36
  • 2
    \$\begingroup\$ @Λ̸̸ good catch, will edit the rules. Yes, it can exceed the digit 9, updating the rules to clarify what to do \$\endgroup\$
    – Dion
    Commented May 9, 2020 at 8:38
  • \$\begingroup\$ Related (the same idea, but different art) \$\endgroup\$ Commented May 9, 2020 at 12:10
  • 21
    \$\begingroup\$ Why does the input have to be from STDIN and out to STDOUT? That seems like an unnecessarily arbitrary restriction. \$\endgroup\$
    – S.S. Anne
    Commented May 9, 2020 at 14:12
  • 4
    \$\begingroup\$ @RossPresser Less interesting than you might think: with input s as a string, l = s.splitlines()[-1]; print(int(l[l.index(' '):]) ** (1 / l.count('/'))). \$\endgroup\$
    – Danica
    Commented May 11, 2020 at 18:31

31 Answers 31

1
2
0
\$\begingroup\$

Java (JDK), 139 171 bytes

String g(int k){return g(0,k);}String g(int i,int n){return i<n?" ".repeat(n-i+1)+"/ ".repeat(i)+"_".repeat((n-i)*2+(""+n).length())+"\n"+g(++i,n):"\\/".repeat(n)+" "+n;}

Try it online!

No for loop, recursion is used. Therefor I had to define the function as a method because I couldn't find a way to define it as a BiFunction lambda expression and call it recursively. The method has 2 inputs: zero as first argument, the integer input parameter as second. And thus added an overloaded method with only one parameter.

Explained

String g(int i,int n) {                          // i is current index (start with 0), n is the input parameter value
  return i<n?                                    // are we not yet at the end?
         " ".repeat(n-i+1)                       // add spaces
          +"/ ".repeat(i)                        // add i times '/ ' 
          +"_".repeat((n-i)*2+(""+n).length())   // add underscores, including extra for the length of the input value
          +"\n"                                  // add CRLF 
          +g(++i,n)                              // recursive call with i+1
         :
         "\\/".repeat(n)+" "+n                   // at last i=n, add \/ + input parameter value
;}                                               // what goes open, must be closed

Called as

System.out.println(g(10));

139 -> 171 : to make it acceptable with only 1 input parameter (@Razetime)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I think you should overload the function to accept a single parameter like this: Try it online! That way it'd be valid. \$\endgroup\$
    – Razetime
    Commented Nov 3, 2020 at 16:20
  • \$\begingroup\$ @Razetime, I'm afraid you're right :(. Added your suggestion to the answer. Thanks for the remark. \$\endgroup\$
    – Conffusion
    Commented Nov 3, 2020 at 16:30
  • \$\begingroup\$ Joys of golfing in a verbose language. You should check out this thread for some shortening methods. \$\endgroup\$
    – Razetime
    Commented Nov 3, 2020 at 16:45
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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