Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Finding the closest perfect square to a number
- CatClawz_
-
500+ posts
Finding the closest perfect square to a number
This is sort of a lead on to a previous topic but it's a new question entirely even though it's related and that topic is a couple of days old so I've made a new one
I want to find the closest perfect square to a number with code. So for example 10 would be 9, 53 would be 49 etc. But without using a script that'll find a square root because the whole point on this code is to find a square root
I want to find the closest perfect square to a number with code. So for example 10 would be 9, 53 would be 49 etc. But without using a script that'll find a square root because the whole point on this code is to find a square root
- Malicondi
-
1000+ posts
Finding the closest perfect square to a number
i made a square root estimator if you want it, but here's a script to find the closest perfect square (if you have a list of every perfect square) but you'll need one variable, mine will be named “i”. The list “sqrts” is the list of all perfect squares from 1 - 100 (for my algorithm) use shift right arrow to see entire script (it's long)
This just finds if the average between the number and square root of the index is greater than the sqrt, and is less than the sqrt above, then finds if the one below or above is higher/lower. The closest sqrt would then be this:
if you want more explanation, let me know!
Last edited by Malicondi (March 4, 2024 15:41:02)
- sashimiricedev
-
100+ posts
Finding the closest perfect square to a number
Couldn't you just round the square root and then square it? In other words,
- Malicondi
-
1000+ posts
Finding the closest perfect square to a number
they're trying to make a square root calculator so you can't just use the square root operator. Couldn't you just round the square root and then square it? In other words,
- CatClawz_
-
500+ posts
Finding the closest perfect square to a number
That was sugested to me, my issue is that if i have a list of perfect squares then it would cut off wherever my list cuts off, so if i only had perfect squares 1-100 then i couldn't find the square of anything over 100 i made a square root estimator if you want it, but here's a script to find the closest perfect square (if you have a list of every perfect square) but you'll need one variable, mine will be named “i”. The list “sqrts” is the list of all perfect squares from 1 - 100 (for my algorithm) use shift right arrow to see entire script (it's long)This just finds if the average between the number and square root of the index is greater than the sqrt, and is less than the sqrt above, then finds if the one below or above is higher/lower. The closest sqrt would then be this:if you want more explanation, let me know!
I will try that method till i find something that won't cut off just so i can started, thanks!
apologies for the slow response I need to stop making topics at the end of the school day when i know i wont be online till the next day lol
Last edited by CatClawz_ (March 5, 2024 09:25:29)
- deck26
-
1000+ posts
Finding the closest perfect square to a number
Of course you could just take the square root of your number. Now square the ceiling of that number and the floor of that number. That gives you the square above and below (assuming they're not the same in which case you had a square already) and you can work out which is nearest. No need to store a list of squares.
Oops, just reread the original post and you said not to use the sqaure root.
But given it is not hard to find a square root without knowing the nearest square I don't understand what this has to do with finding a square root. The normal method gets close quickly enough.
Using a list does assume you're working within a specified range or you're going to have to expand the list.
Oops, just reread the original post and you said not to use the sqaure root.
But given it is not hard to find a square root without knowing the nearest square I don't understand what this has to do with finding a square root. The normal method gets close quickly enough.
Using a list does assume you're working within a specified range or you're going to have to expand the list.
Last edited by deck26 (March 5, 2024 10:29:53)
- Malicondi
-
1000+ posts
Finding the closest perfect square to a number
you can change i to be i*i, but it might be a little slower because the algorithm has to do more math, here's the new block if you do so: That was sugested to me, my issue is that if i have a list of perfect squares then it would cut off wherever my list cuts off, so if i only had perfect squares 1-100 then i couldn't find the square of anything over 100
I will try that method till i find something that won't cut off just so i can started, thanks!
apologies for the slow response I need to stop making topics at the end of the school day when i know i wont be online till the next day lol
doesn't have a limit like lists, but lists are faster
Last edited by Malicondi (March 5, 2024 11:25:40)
- deck26
-
1000+ posts
Finding the closest perfect square to a number
If the last item in the list is less than your value you add more in a loop until the last item is not less than your value.
- Discussion Forums
- » Help with Scripts
-
» Finding the closest perfect square to a number