wu :: forums
« wu :: forums - perfect squares in a sorted aaray »

Welcome, Guest. Please Login or Register.
Nov 28th, 2024, 10:51am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Grimbal, SMQ, ThudnBlunder, towr, Icarus, Eigenray, william wu)
   perfect squares in a sorted aaray
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: perfect squares in a sorted aaray  (Read 1239 times)
srikanth.iiita
Newbie
*





   


Posts: 33
perfect squares in a sorted aaray  
« on: Mar 31st, 2010, 8:23am »
Quote Quote Modify Modify

given an sorted array ...
find all numbers which are perfect squares...
propose the best possible method
« Last Edit: Mar 31st, 2010, 8:29am by srikanth.iiita » IP Logged
SMQ
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 2084
Re: perfect squares in a sorted aaray  
« Reply #1 on: Mar 31st, 2010, 8:27am »
Quote Quote Modify Modify

Pretend you have a second sorted list containing only perfect squares and use the standard algorithm for finding the common elements of the two lists.  Rather than actually maintaining a pointer into the pretend list, merely increment an integer when you would advance the pointer.

 
--SMQ
IP Logged

--SMQ

srikanth.iiita
Newbie
*





   


Posts: 33
Re: perfect squares in a sorted aaray  
« Reply #2 on: Mar 31st, 2010, 8:36am »
Quote Quote Modify Modify

constant memory solution plz...
can  we take the help of binary search .. here
IP Logged
SMQ
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 2084
Re: perfect squares in a sorted aaray  
« Reply #3 on: Mar 31st, 2010, 8:43am »
Quote Quote Modify Modify

The solution above is constant memory, and O(n + MAX(A)) time which is approximately O(n) unless A is very sparse.
 
--SMQ
« Last Edit: Mar 31st, 2010, 8:44am by SMQ » IP Logged

--SMQ

Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: perfect squares in a sorted aaray  
« Reply #4 on: Mar 31st, 2010, 8:48am »
Quote Quote Modify Modify

That IS constant memory.
 
You just pretend to have a second array with squares.  Actually the values are computed on the fly.
 
 
(This goes against what I heard, that when you start pretending things, you better have a good memory).
 
PS: as SMQ explained faster than me.
« Last Edit: Mar 31st, 2010, 8:49am by Grimbal » IP Logged
spicy
Newbie
*





   


Gender: female
Posts: 8
Re: perfect squares in a sorted aaray  
« Reply #5 on: Apr 7th, 2010, 8:39am »
Quote Quote Modify Modify

Will it still be a good approach if elements have huge variations?
like
2,4444,2309129 etc
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: perfect squares in a sorted aaray  
« Reply #6 on: Apr 7th, 2010, 9:05am »
Quote Quote Modify Modify

You can use floor(sqrt(N))2==n to check squareness.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
william wu
wu::riddles Administrator
*****





   
WWW

Gender: male
Posts: 1291
Re: perfect squares in a sorted aaray  
« Reply #7 on: Apr 7th, 2010, 4:53pm »
Quote Quote Modify Modify

on Apr 7th, 2010, 9:05am, towr wrote:
You can use floor(sqrt(N))2==n to check squareness.

 
Seems like this would be a better approach if we have huge variations, since otherwise we'd be incrementing the pretend array for a long time.
 
In[1]:= (Floor[Sqrt[#]]^2 == #) & /@ {3, 13, 64, 15, 36, 19}
Out[1]= {False, False, True, False, True, False}
 
However, this approach also requires floating point arithmetic, to take the square root -- whereas SMQ's solution does not.
 
I read about an integer-based square root test here:
http://www.advogato.org/person/fxn/diary.html?start=465
IP Logged


[ wu ] : http://wuriddles.com / http://forums.wuriddles.com
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: perfect squares in a sorted aaray  
« Reply #8 on: Apr 8th, 2010, 12:45am »
Quote Quote Modify Modify

You can also do binary search for the next square smaller or equal to the next candidate in the array.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
TenaliRaman
Uberpuzzler
*****



I am no special. I am only passionately curious.

   


Gender: male
Posts: 1001
Re: perfect squares in a sorted aaray  
« Reply #9 on: Apr 10th, 2010, 8:08am »
Quote Quote Modify Modify

You can have an increment lookup table. Look at the current square value of the pretend array and the next y value in the give array. If the difference is greater than some N, choose an appropriate increment value.
 
You can even have a function that computes the best possible increment.
 
y0 = x0^2
y1 = (x0 + Dx)^2
Dy = 2 * x0 * Dx + Dx^2
Dx = (- 2 * x0 +/- sqrt(4 * x0^2 + 4 * Dy)) / 2
x0 + Dx = sqrt(x0^2 + Dy)
 
One just needs an integer root here, which can computed without resorting to floating point arithmetic.
 
However, having the lookup tables might be faster than the above.
 
on Apr 7th, 2010, 4:53pm, william wu wrote:
Seems like this would be a better approach if we have huge variations, since otherwise we'd be incrementing the pretend array for a long time. <snip>

IP Logged

Self discovery comes when a man measures himself against an obstacle - Antoine de Saint Exupery
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board