22
\$\begingroup\$

Challenge:

Given two integers \$a\$ and \$b\$, with lengths \$A=length(a), B=length(b)\$, output an ASCII-art of the \$a^{th}\$ root of \$b\$, including the answer rounded to \$A\$ amount of decimal places.
The size of the ASCII-art root also depends on \$A\$ and \$B\$.

Example:

\$a=123\$, \$b=1234\$

     ____
123 /1234 = 1.060
\  /
 \/

Because \$B=4\$, we have four _ above the 1234. Because \$A=3\$, we have three / and \$1.0595772951...\$ is rounded to 1.060.

: This will not be equal to \$A\$, but \$\left\lceil{\frac{A}{2}}\right\rceil+1\$ instead.

Challenge rules:

  • I/O format is flexible:
    • You're allowed to take the input loose or as a pair of integers; doubles; strings; list of digits; etc.
    • You're allowed to print the result directly to STDOUT; return it as a string; return it as a list/array/stream of string-lines; return it as a matrix of characters; etc.
  • If input \$a\$ has an odd length \$A\$, the number is left-aligned in the output. So in the example above you're not allowed to have the second line as 123/1234 = 1.060! (It should be 123<space>/... instead of <space>123/...)
  • The = in the output should be surrounded by a single leading/trailing space
  • Rounding can be done in any reasonable way (e.g. rounding towards 0, half-up, half-even, banker's rounding, etc.)
  • If the rounded result ends with 0s, you're allowed to omit them. (E.g., the 1.060 in the example above may also be 1.06.)
  • You can assume \$a>0\$ and \$b\geq0\$ (\$a\$ is positive, \$b\$ is non-negative).
  • Trailing spaces in the output, and a single trailing newline is fine. Multiple trailing newlines or trailing whitespaces are not.

General rules:

  • This is , so the shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code (e.g. TIO).
  • Also, adding an explanation for your answer is highly recommended.

Test cases:

Inputs: a=3, b=100 (A=1, B=3)
   ___
3 /100 = 4.6
\/

Inputs: a=123, b=1234 (A=3, B=4)
     ____
123 /1234 = 1.060
\  /
 \/

Inputs: a=10, b=10 (A=2, B=2)
   __
10/10 = 1.26
\/

Inputs: a=1298123, b=9023847978341 (A=7, B=13)
         _____________
1298123 /9023847978341 = 1.0000230
\      /
 \    /
  \  /
   \/

Inputs: a=2500, b=0 (A=4, B=1)
     _
2500/0 = 0.0000
\  /
 \/
\$\endgroup\$
6
  • \$\begingroup\$ Related: Square Root of ASCII art. \$\endgroup\$ Commented May 9, 2022 at 13:30
  • \$\begingroup\$ What's the policy on trailing whitespace? \$\endgroup\$
    – loopy walt
    Commented May 9, 2022 at 15:42
  • \$\begingroup\$ @loopywalt Trailing spaces and a single trailing newline is fine. I'll add it. \$\endgroup\$ Commented May 9, 2022 at 17:16
  • \$\begingroup\$ Do you mean to assume that \$a\$ is a positive integer and \$b\$ is a nonnegative integer? \$\endgroup\$ Commented May 10, 2022 at 8:46
  • 1
    \$\begingroup\$ @GregMartin Yes, isn't that exactly what I stated in rule "You can assume \$a>0\$ and \$b\geq0\$ (\$a\$ is positive, \$b\$ is non-negative)."? \$\endgroup\$ Commented May 10, 2022 at 9:20

8 Answers 8

9
\$\begingroup\$

Python, 143 bytes

def f(a,b):
 A=len(str(-a))&-2;print(A*" ",len(str(b))*"_"+f"\n{a:<{A}}/{b} = {b**(1/a):.{A}}");p="\\"
 while A:A-=2;print(p+A*" "+"/");p=" "+p

Attempt This Online!

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Smart to use len(str(-a)) instead of len(str(a))+1 for -1 byte! :) \$\endgroup\$ Commented May 9, 2022 at 17:20
6
\$\begingroup\$

JavaScript (ES8),  158  156 bytes

Expects (a)(b), where both arguments are strings.

a=>b=>" "[R='repeat'](q=(A=a.length)+A%2)+` ${"_"[R](b.length)}
${a.padEnd(q)}/${b} = ${(b**=1/a).toFixed(A)}
`+(g=p=>q?p+" "[R](q-=2)+`/
`+g(" "+p):"")`\\`

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Charcoal, 37 bytes

θ⸿↘⊘⊕Lθ↑↗⊘⁺³Lθ⟦⭆η_⟧η = ﹪⁺⁺%.LθfXIη∕¹N

Try it online! Link is to verbose version of code. Explanation:

θ

Output the first input.

⸿↘⊘⊕Lθ↑↗⊘⁺³Lθ⟦⭆η_⟧

Draw the square root sign.

η = ﹪⁺⁺%.LθfXIη∕¹N

Output the second input and the formatted result.

\$\endgroup\$
6
\$\begingroup\$

Lua, 205 203 188 bytes

a,b=...p,c,s=print,a.rep,a.len d=s(a)r=d%2+d+1 p(c(" ",r)..c("_",s(b)))p(a.format("%s%s/%s = %."..d.."f",a,c(" ",d%2),b,b^(1/a)))for i=1,r//2 do p(c(" ",i-1).."\\"..c(" ",r-i*2-1).."/")end

Attempt This Online! Explanation soon:tm: once I'm out of school. I added an explanation to the footer of the ATO link. enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ @KevinCruijssen Should be good now. \$\endgroup\$
    – twentysix
    Commented May 9, 2022 at 15:44
  • \$\begingroup\$ Yep, looks good now. +1 from me. :) \$\endgroup\$ Commented May 9, 2022 at 17:18
5
\$\begingroup\$

Python 3.8 (pre-release), 161 bytes

lambda a,b:f"{' '*(Z:=(A:=len(str(a)))+1&-2)} {'_'*(B:=len(str(b)))}\n{a:<{Z}}/{b} = {b**a**-1:.{A}f}"+"".join(f"\n{' '*i}\\{' '*(Z+2*~i)}/"for i in range(Z//2))

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Julia 1.0, 137 bytes

!x=length("$x")
a\b=[" "^-~(X=!a+!a%2)*"_"^!b;"$a"*" "^(r=1:X÷2;!a%2)*"/$b = $(round(b^a^-1,digits=!a))";@. ' '^~-r*"\\"*" "^(X-2r)*'/']

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Python, 169 bytes

def f(a,b):print(" "*((A:=len(str(a)))-1+(x:=A%2)),"","_"*len(str(b))+f"\n{a}{x*' '}/{b} =",round(b**1/a,A),*("\n"+i*" "+"\\"+(A-i-i-2+x)*" "+"/"for i in range(A//2+x)))

Attempt This Online!

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

C (gcc), 222 218 bytes

-4 bytes thanks to @ceilingcat

#define P;printf(
#define X snprintf(0,0,"%.0f"
A;B;f(double a,double b){A=X,a)P"  %*c",A+A%2,95);for(B=X,b);--B P"_"))P"\n%.0f%*c%.0f = %.*f",a,1+A%2,47,b,A,pow(b,1/a));for(A-=~A&1;B++<A--P"\n%*c%*c",B,92,2+A-B,47));}

Try it online!

\$\endgroup\$
0

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.