13
\$\begingroup\$

Many formulas in math involve nested radicals (square root signs inside other square roots signs). Your task here is to draw these using ascii art.

Specs

You will be given two integers, the number inside the radical, and the number of radicals. I think the best way to explain what you have to do is with an example. Here is the output for 2, 4:

        ____________________
\      /       _______________
 \    /  \    /     __________
  \  /    \  /  \  /   _____
   \/      \/    \/  \/ 2

Here are some things to note:

  • The height of each radical increases by one
  • The length of the _'s are always 5 times the height
  • There is a space after the last / and before the next inner radical starts
  • The number inside will never be greater than 4 digits
  • If the number of radicals is 0, just output the number
  • Putting extra spaces to pad it into a rectangle is up to you
  • This is , so shortest code in bytes wins!

Test Cases

2, 4:
        ____________________
\      /       _______________
 \    /  \    /     __________
  \  /    \  /  \  /   _____
   \/      \/    \/  \/ 2

23, 0:
23

4, 1:
  _____
\/ 4

1234, 3:
      _______________
\    /     __________
 \  /  \  /   _____
  \/    \/  \/ 1234
\$\endgroup\$
1
  • 12
    \$\begingroup\$ I feel like this would be a slightly better challenge if the horizontal bars all had to end at the same point. \$\endgroup\$ Dec 20, 2016 at 0:13

4 Answers 4

2
\$\begingroup\$

Python 3.5, 145 137 bytes

def s(n,x):[([print(' '*j+'\\'+'  '*i+'/ '+' '*j,end='')for j in range(x-i-1,-1,-1)],print('  '*i+i*'_____'or n))for i in range(x,-1,-1)]

Slightly ungolfed:

def s(n,x):
    for i in range(x,-1,-1):
        for j in range(x-i-1,-1,-1):
            print(' '*j+'\\'+'  '*i+'/ '+' '*j,end='')
        print('  '*i+i*'_____' or n)

Output:

s(2,4)
        ____________________
\      /       _______________
 \    /  \    /     __________
  \  /    \  /  \  /   _____
   \/      \/    \/  \/ 2
\$\endgroup\$
2
  • \$\begingroup\$ print(' '*i+'_____'*i or n) saves 7 bytes. EDIT print(' '*i+i*'_____'or n) saves 8. \$\endgroup\$ Dec 20, 2016 at 4:05
  • \$\begingroup\$ Thank you. I did not know you could use 'or' like that. \$\endgroup\$ Dec 20, 2016 at 13:19
2
\$\begingroup\$

JavaScript, 133 132 131 bytes

f=(n,r,q=r)=>~r?'1\\0/1 '[x='repeat'](d=q-r).replace(/\d/g,i=>' '[x](+i?d-=.5:r*2))+(r?' '[x](r*2)+'_'[x](5*r):n)+`
`+f(n,r-1,q):''


F=(n,r)=>console.log( f(n,r) )
F(2,4)
F(23,0)
F(4,1)
F(1234,3)
.as-console-wrapper{max-height:100%!important;top:0}

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

JavaScript (ES6), 124 bytes

f=(s,n,i=0,r=(n,c=` `)=>c.repeat(n))=>n?r(n+n)+r(n*5,`_`)+`
`+f(s,n-1).replace(/^/gm,_=>r(i)+`\\${r(n+~i<<1)}/`+r(++i)):s+``
<div oninput=o.textContent=f(s.value,+n.value)><input id=s><input id=n type=number min=0><pre id=o>

Save 3 bytes if the first parameter can be a string rather than a number.

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

PHP, 178 bytes

for($r=[" $argv[1]"];$i++<$argv[2];$r[]=$p("",2*$i).$p(_,5*$i,_))for($k=-1;++$k<$i;)$r[$k]=($p=str_pad)("\\".$p("",2*$k)."/",2*$i," ",2).$r[$k];echo join("\n",array_reverse($r));

bah that´s awfully long.

\$\endgroup\$

Your Answer

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

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