Calculating square root of any number using a lower and upper bound that guesses towards solution.
how? ;-;
30 Replies
What have you tried?
Nothing, because im trying to figure out how would i achieve this:
like i geniunenly dont get what lower or upper bound its asking for?
and how id used it to find square root of any number
Ok. Let's say I tell you "give me the square root of 483269867.
What initial bound do you think could mean / be?
well the next closeset square number that has a square root number close to that? so for example square root of 50, would have square root of 49 the next closest, which is 7^2
right?
Not quite. You're assuming you know about 7^2
What I was expecting is "the square root has to be between 0 and 483269867"
Makes sense?
mhmmmm yes i guess
Well, not 483269867. 483269867 + 1.
So basically, you know that it's above 0, and you know it's bellow V + 1.
hmm okay, so maybe between 483269867 - 1 and 483269867 + 1?
that -1 is lower ound
and +1 upper
No, it doesn't work for the lower bound.
smh
The square root is lower than V - 1
Ohhh okay true true
Let's compute the square root of 4.5 using the method, it will be simpler.
You know it's between 0 and 5.5 (excluded)
What you then do is you take a number between those, you square it. (do it (manually), I'm waiting)
wait why 5.5 and not like 5?
(Ideally, you would take the mid-point, not random)
Any number higher by 0.25 would do I think.
ah ok fair enough, btw i think im onto something, so i then do 0 + 5.5 and divide it by 2, and then i square that and see how close i am to 4.5?
So yes, the bound can start at 0 and V + 0.25
Yup. If you're above, you move the upper bound to what you tried. If you're bellow, you move the lower bound to what you tried.
Holy crap, wait wait so
oooooo
And you do it as long as
Math.Abs(v1 - v2) > tolerance
.
(don't use these variable names, it's just for illustrative purposes)mhmmmm i seee, so if its above 4.5....
then i try 0 + what i just tried
/ 2 and thne squared right?
Yup
Holy moly you a genius, id have never thought of that lol
i couldnt find solutions to this anywhere as well
Oh holy crap you even made the code in like 10 seconds!
Yawnder#7904
REPL Result: Success
Console Output
Compile: 664.938ms | Execution: 36.923ms | React with ❌ to remove this embed.
that is hella impressive, btw what does the M stand for tho?
To indicate that the number preceding it is a decimal.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types#real-literals
Ahhh i see
that code is so much simpler than i thought it would have been ngl
i thought i needed like tons of math
There probably are more efficient ways to compute that, but that's how I would do it based on the question you asked.
hey i wouldnt have thought of anything lol, so that will 100% do, especially when i couldnt find any solutions to this problem online