8

EDIT: Thanks to everyone who answered, but I did not do a good enough search for questions like mine and in fact there was someone else who asked the exact same question. The second linked question is essentially the same as mine and even has similar answers. Thanks!

I'm using a website called Anydice, and they allow some basic math operations, but do not have square roots. I would like to use a square root but now I'm more interested in trying to make one with the operations they have. The website can do:

• Addition

• Subtraction

• Multiplication

• Integer division $\left(\frac{6}{3 }= 2\right.$, while $\frac{1}{2} = 1$. The answers are always integers.$\left.\right)$

• And Exponentiation

Additonally, it doesn't seem to like decimals, only integers are allowed. This means I can't do something like $4^\left(\frac{1}{2}\right)$ or $4^\left(0.5\right)$.

In additon to those operations, the program can:

• Create and edit variables

• Enter and exit loops

• Compare values $\left(\gt, \lt, =, \ge, \le\right)$

• Create conditionals (if, then, else)

• Create functions of code

Is it possible, with this set of rules, to create a square root? Can I find $\sqrt{25}=5$ in Anydice?

EDIT: I am aware that a square root can be represented by $x^{\frac{1}{2}}$ or $x^{0.5}$, as well as the fact that $\sqrt x$ can be computed as $10^\left(\frac{log_{10}x}{2}\right)$. The first two are rejected for reasons explained above, while the last example requires an operation I don't have access to. My research showed that computing the log function is usually done by addition of infinite sets of fractions, which is also impossible.

I'm already starting to think that this cannot be done, but I'm interested enough to wait and see if anyone can think of anything.

  • 2
    Can variables be rational numbers? If so, then just use the fact that $\sqrt x = x^{0.5}$ – 5xum Mar 23 '23 at 09:25
  • You're right, and I totally forgot to mention that it doesn't seem to like decimals. The program is used for dice and probability calculations but it can do math with those calculations as well. It just seems that the creator didn't expect square roots or decimals to be important for that. I have updated the question to clarify. – Jack Foisy Mar 23 '23 at 15:56
  • Does it allow equations like $x^2 = 2$? So that we can use $x$ later? @JackFoisy – D S Mar 23 '23 at 16:18
  • Do you know that you can compute square roots quickly to high precision with Heron's Method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Heron's_method) which is a special case of Newton's Method? But you need fractions for that, obviously ... – Torsten Schoeneberg Mar 23 '23 at 16:23
  • It's rather odd to me that a computation system designed to compute probabilities of various dice rolling situations is so uninterested in standard deviations as to have no built-in square root capability. – Lee Mosher Mar 23 '23 at 16:50
  • 2
    There's a method to calculate square roots by hand that you could adapt to your purposes. Method 2 of https://www.wikihow.com/Calculate-a-Square-Root-by-Hand explains it, though it might be better for you to find a different link to explain it. I will say that it's based on the fact that $(10a+b)^2 = 100a^2 + (102a+b)b$ – gist076923 Mar 23 '23 at 17:38
  • If the website allows rational indices then just use $\sqrt{y}=y^{\frac12}$ – MathStackexchangeIsNotSoBad Mar 23 '23 at 09:54
  • Are you sure that your answer will be an integer (like √25), or could it end up with an irrational (like √24)? And what would you want in the latter case? (And how is it deciding what to do with integer division?) – Teepeemm Mar 23 '23 at 18:13
  • "It doesn't seem to like decimals"... then how do you expect to get it to output $\sqrt 2$ which is not a whole number? – 5xum Mar 24 '23 at 07:23
  • @5xum You output the "integer square root", which is (near as I can tell) usually defined as the largest integer $m$ such that $m^2 \le n \le (m+1)^2$. It seems that this is something which computer scientists have thought a lot about. – Xander Henderson Mar 24 '23 at 12:54
  • @XanderHenderson In that case the value can easily be found using a simple for loop. Not an efficient way to do it, but it's a way, and OP did not ask for efficiency. – 5xum Mar 24 '23 at 12:59
  • 1
    @5xum Yes, and that is the answer given at https://math.stackexchange.com/a/4664936/ . But the linked questions give other algorithms. – Xander Henderson Mar 24 '23 at 13:02

1 Answers1

6

Variables and loops, you say?

r = 0
for(i goes from 0 to n)
   if (r+1)*(r+1) <= n
       r = r + 1

When the loop exits, $r$ will be $\sqrt n$, rounded down. It's slow and stupid, but it'll get the job done. I'm also guessing you're only using it for small values of $n$, so the inefficiency won't be too painful.

The code's written assuming you have only the most basic loop constructs (i.e. no while loops, fixed number of iteration for loops, no breaking out of loops.) You can make this a bit nicer if you can use any of those, but it's still "count up one at a time, and stop when $r*r$ gets larger than $n$".

JonathanZ
  • 10,615
  • 2
    No, it's psuedo-code. (I rewrote the for loop so it looks less like any particular language.) – JonathanZ Mar 23 '23 at 16:40
  • I think one could use clever math and for example try to implement Newton's method or something like that but from OPs description I think this is totally sufficient and very simple. – quarague Mar 23 '23 at 17:45
  • 1
    @quarague - that would be interesting, but as everything has to be done with integers there's a risk of exact square roots, like $\sqrt 25$, coming out just below 5, and being reported as 4. As utterly inelegant as this is, at least it should handle 9,16, 25, 36, etc. correctly. – JonathanZ Mar 23 '23 at 18:03