Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

while loop perfect square

280 views
Skip to first unread message

Raymond

unread,
Sep 29, 2010, 4:46:22 PM9/29/10
to
I'm trying to write a while loop to count the number of perfect squares between 1 and N. Let's set the number of perfect squares as the variable y.

x=1;
y=0;
while x:N
if mod(sqrt(x), 1)==0;
y=y+1
end
x=x+1
end

This is roughly what I'm trying to do. Can you guys show me my errors and how to fix them? Thanks as always.

Raymond

unread,
Sep 29, 2010, 4:46:22 PM9/29/10
to

ImageAnalyst

unread,
Sep 29, 2010, 5:09:48 PM9/29/10
to
Your while statement was wrong. You wanted x<N, not x:N (which is a
long vector). Try this:

N = 70;
x = 1;
while x <= N
if rem(sqrt(x), 1) <= eps
fprintf('%d is a perfect square.\n', x);
else
fprintf('%d is NOT a perfect square.\n', x);
end
x=x+1;
end

Raymond

unread,
Sep 29, 2010, 5:12:05 PM9/29/10
to
nevermind figured it out

Raymond

unread,
Sep 29, 2010, 5:12:05 PM9/29/10
to
nevermind figured it out. double post was a mistake. computer lagging. no intent

Royi Avital

unread,
Sep 29, 2010, 7:35:20 PM9/29/10
to
"Raymond " <atlantasw...@yahoo.com> wrote in message <i808iu$723$1...@fred.mathworks.com>...

I would go:

x = 1;
y = 0;


while x <= N
if rem(sqrt(x), 1) <= eps

y = y + 1;
x = x + sqrt(x);
end
x = x + 1;
end

Since if x is a perfect square no other "Perfect Square" would be in sqrt(x) range.

ImageAnalyst

unread,
Sep 29, 2010, 8:32:12 PM9/29/10
to
Yeah, nice trick to speed it up. But since no floating point number
would be a perfect square of an integer, wouldn't you do

x = x + ceil(sqrt(x));

For even more speedup, we could do
x=(sqrt(x)+1)^2 - 1 % ;-)


and I still don't know why that "y" is in there.

Royi Avital

unread,
Sep 29, 2010, 9:08:24 PM9/29/10
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <4d675c46-e9fd-491b...@i5g2000yqe.googlegroups.com>...

Yea, You got where I got my Speed Up idea from :-).

It is recommended to use you corrections.

Matt Fig

unread,
Sep 29, 2010, 9:40:24 PM9/29/10
to
This might be faster, yet still a WHILE loop.

count = 1;
while count<= sqrt(N)-1
count = count + 1;
end

Steven_Lord

unread,
Sep 30, 2010, 2:07:22 PM9/30/10
to

"Matt Fig" <spam...@yahoo.com> wrote in message
news:i80pq8$g8q$1...@fred.mathworks.com...

If you were looking to do this without SQRT, you could use the second
equation given on this page:

http://en.wikipedia.org/wiki/Square_number

function numSquares = countsquares(N)
numSquares = 0;
currentSquare = 1;
increment = 3;
while currentSquare <= N
numSquares = numSquares + 1;
currentSquare = currentSquare + increment;
increment = increment + 2;
end

This handles negative and noninteger values for N; add error checking for
complex, nonfinite, or nonscalar N to taste.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Matt J

unread,
Sep 30, 2010, 3:09:20 PM9/30/10
to
"Raymond " <atlantasw...@yahoo.com> wrote in message <i808iu$723$1...@fred.mathworks.com>...
> I'm trying to write a while loop to count the number of perfect squares between 1 and N.
====

Why? Isn't the answer always floor(sqrt(N))

raman

unread,
Nov 20, 2010, 6:11:53 AM11/20/10
to
write a c program to display perfect square &cube?


ImageAnalyst

unread,
Nov 20, 2010, 8:54:16 AM11/20/10
to
On Nov 20, 6:11 am, raman <u...@compgroups.net/> wrote:
> write a c program to display perfect square &cube?
-------------------------------------------------------------------------------------
You write a sentence in the imperative mood (http://en.wikipedia.org/
wiki/Grammatical_mood) yet you add a question mark indicating it's in
the interrogative mood. I have no idea if you're demanding that we
perform some task for you, or if you're asking us a question.

Steven_Lord

unread,
Nov 21, 2010, 6:38:37 PM11/21/10
to

"ImageAnalyst" <imagea...@mailinator.com> wrote in message

news:3e598046-0b28-431d...@p1g2000yqm.googlegroups.com...

In either case, since the message refers to C but not MATLAB, the message
should probably have been posted on a different newsgroup like comp.lang.c
(or should not have been posted at all.)

--
Steve Lord
sl...@mathworks.com

0 new messages