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 be123<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., the1.060in the example above may also be1.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 code-golf, 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
\ /
\/
-
\$\begingroup\$ Related: Square Root of ASCII art. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年05月09日 13:30:15 +00:00Commented May 9, 2022 at 13:30
-
\$\begingroup\$ What's the policy on trailing whitespace? \$\endgroup\$loopy walt– loopy walt2022年05月09日 15:42:41 +00:00Commented May 9, 2022 at 15:42
-
\$\begingroup\$ @loopywalt Trailing spaces and a single trailing newline is fine. I'll add it. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年05月09日 17:16:35 +00:00Commented 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\$Greg Martin– Greg Martin2022年05月10日 08:46:13 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2022年05月10日 09:20:22 +00:00Commented May 10, 2022 at 9:20
8 Answers 8
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
-
4\$\begingroup\$ Smart to use
len(str(-a))instead oflen(str(a))+1for -1 byte! :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年05月09日 17:20:34 +00:00Commented May 9, 2022 at 17:20
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):"")`\\`
Charcoal, 37 bytes
θ⸿↘⊘⊕Lθ↑↗⊘+3Lθ⟦⭆η_⟧η = %++%.LθfXIη∕1N
Try it online! Link is to verbose version of code. Explanation:
θ
Output the first input.
⸿↘⊘⊕Lθ↑↗⊘+3Lθ⟦⭆η_⟧
Draw the square root sign.
η = %++%.LθfXIη∕1N
Output the second input and the formatted result.
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
-
1\$\begingroup\$ @KevinCruijssen Should be good now. \$\endgroup\$twentysix– twentysix2022年05月09日 15:44:52 +00:00Commented May 9, 2022 at 15:44
-
\$\begingroup\$ Yep, looks good now. +1 from me. :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年05月09日 17:18:30 +00:00Commented May 9, 2022 at 17:18
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))
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)*'/']
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)))
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));}