Problem 23. Finding Perfect Squares
Solution Stats
Problem Comments

34 Comments
Another of the classic "check if MATLAB has a function that does this already" problems...
What about a vector like [2 3 1]? The top solution will still return true because 1^2 is still one but it is not the square of one of the OTHER numbers.
is the Str2func construct really more effective???
Is it possible that this distorts the metric a bit?
It is currently impossible to submit a solution for this problem. The web page will never load.
The problem description states that one of the numbers has to be a square of one of the OTHER numbers. Still, the test suite tests if [1] gives true. I think [1] should give false, or that describtion be modified.
Agree with Mattias. the length of input vector should be 2 or larger.
The difference between test 4 and 5 which returns different answers is the number 36.. but 36 is not a square root of any number of the list in test 4!!
Agree with Mattias: the Test Suite is defective, and not consistent with the problem specification.
Having an input of [1] is OK, but result should be false, as there is no OTHER element to compare the 1 to!
easy
Fun question!
the word "other" should be ignored in the description...
fun
Good question!
Clever problem
good
The heading reads:
"...a square of one of the OTHER numbers...".
How can the function be expected to return [true] for a = [1]?
It makes no sense.
Good question
This problem is very good
intersect的返回值为一个向量，即使a和b的交集为空集，返回值仍为向量
x=[20:30]
例如：intersect(x.^2,x)
ans =
空的 1×0 double 行向量
所以isvector(intersect(x.^2,x))是错误的
而isempty是判断数组是不是空的，所以isempty可行
Tough one, but interesting
I solved it with 2 forloops, 1 function, 1 ifcondition.
It works, but there are more efficient ways apparently
Hey,the demand of this problem declare that "if one " and "the other",so if the intersect of x and x.^2 only have one value of 0 or 1,it may be return false because their squares are themselves, which is not satisfy the condition implied that c and c^2 is not one.
Nice Problem
The question asks if one number is the square of one of the OTHER numbers. Test 3 should return false.
Great problem
Nice
error in test 7 ! You missed 'd' in function.
fix it!!!!
Error has been fixed.
Good problem
Good beginners' question!
So far it is the most time consuming problem for me.
function b = issqr(a);
x = a.^2;
issqr = x;
c = ismember(a,x);
if any(c) == 1
'true'
else
'false'
end
end
Not sure why this isnt working any help would be appreciated
@Max Weinburg, you have to output logical values not string/char
Great problem for introducing to MATLAB
Solution Comments

1 Comment
too difficult with only documentation

1 Comment
The last if is just a way to tell the function to say false, as I was getting desperate :) but the logical indexing works!

2 Comments
You should have stated upfront that if statements are not allowed.
These bans are sometimes used, I believe, to keep people from submitting "cheat" solutions that simply check which test case they're being fed and produce the corresponding output.
As you observe, however, this is often overzealous in that in many problems, case distinctions arise naturally in the solution. (Not to mention that the letters "if" may well appear together in a solution that doesn't even use if statements.)
And I agree, if such restrictions are imposed they should be communicated clearly.

1 Comment
nice!

1 Comment
This code works only if the vector is ordered in an ascending order.
If we have for example [4 3 2] it will not work, need a sqrt check then. Or even worse, if the vector is not ordered at all!

1 Comment
tricky

1 Comment
very challenging problem!!

3 Comments
I don't understand why #4 if false. Can someone help me?
Test case #4 is true, not false, because the vector contains a perfect square and its square root.
Your function is not passing because it overwrites the check variable for each vector element, only actually checking the last value to return the true/false variable. It happened to pass the other four cases because their answers (either false or true) happen to correlate with your check for the last (or only) element in the vector.
GOOD

2 Comments
a = [20:30];
assert(isequal(isItSquared(a),false))
Assertion failed.
It should be true because in the array there is 25.
a = [20:30]
a = 20 21 22 23 24 25 26 27 28 29 30
The question asks that whether a perfect square and it's root both are present in the array or not. You have to check for both and not one only.

1 Comment
Hi,
I verified this code with the given test suite. but the system is not accepting the code as it is.
thanks
TR

1 Comment
What's wrong with this code? Runs perfectly on my computer.

1 Comment
I didn't see the leading code,so I put my code here to give some inspiration.
Looking forward to a shorter one
b=~isempty(intersect(a,a.^2));

1 Comment
whoops, don't know how I managed to confuse .^ with .* for a bit there I was like, "What's WRONG?"

1 Comment
nice

2 Comments
You cheater!
This type of cheat/hack answers are no longer allowed.

2 Comments
Good question
0 and 1 elements in a will show the bug in your code

2 Comments
nice oneliner
a vector 'a' with elements 0 or 1 will show the bug in your code

1 Comment
cool!

2 Comments
Wow! This took me a couple of days to solve, but I did it and I’m very proud! :D
Hi. A vector with ones or zeroes as v = [3, 1] will show the bug in your code.

1 Comment
Lol

3 Comments
Test case : a = [20:30] contains a square. but its expected error is false. It should be fixed
hello,
where do you find a perfect square in [20:30]?
Hi Jann B,
25 is a perfect square, no?

1 Comment
Very interesting solution!

2 Comments
How is the output true if the vector contains only one number? People posting such meaningless solutions: Please have some common sense!
If that one number is 1, then the vector contains both a number and its square.

1 Comment
easy easy

2 Comments
That's better...
a vector as v = [3,2,7,0,5] will show you your code's bug

2 Comments
a little smaller now
Nice ☺

1 Comment
Works

1 Comment
just kidding...

1 Comment
very easy

1 Comment
I want to make this code shorter!!

1 Comment
Case2,a = [20:30],fails.
Others pass.
Why???

1 Comment
in 2 test suite in a=[20:30] , contains 5^2 =25 so it has to be true not false

1 Comment
But there seems to be a better choice. Why forgetting ismember?

2 Comments
Test 2 in the test suite is wrong!
25 is clearly a perfect square.
the problem is to see if a number is the square of another number in the vector. Although 25 is a perfect square, 5 is not included in the vector

2 Comments
Though it passes the test suite, I feel like I could make this a better way but I am drawing a blank. Any feedback would be appreciated.
good

3 Comments
if a = [20:30] the output should be true as 25 is a perfect square, while in the test case output is given to be false.
25 is not a square of any other members of the group. If the group included 5 it would be true.
hur dur durrr durr. sqrt(25)==5, not included in givens

1 Comment
Yeah, I did it

2 Comments
My solution returns false for [20:30]; why not for [6 10 12 14 101]?
Instead of a(1):a(end) you should just write a.

2 Comments
Where did I do wrong？
The desired results are not the character strings 'true' and 'false' but the logical values true and false.

1 Comment
hint ; any and ismember.

1 Comment
submitted tested solution but still showing error......
L 17 (C 13): Parse error at END: usage might be invalid MATLAB syntax.

2 Comments
interesting solution... I like it :)
I disagree. Almost every problem has a solution wrapped in this function. It's cheating as it hides the complexity of the code from the scoring algorithm. That's why "eval" is not allowed ...

1 Comment
This is the correctly executing in matlab

1 Comment
I wonder...if you cheat, why even bother to actually solve the problem?

1 Comment
I have not heard of ismember before, thanks.
As primitive as my solutions are, I learn just as much from reading other peoples as doing the problem itself. Great setup mathworks :)

1 Comment
Code works on Matlab, but not on your test suite?

1 Comment
Srtr2func, classic way:)a new thing learned.

2 Comments

1 Comment
test case 3 should be 'false'

1 Comment
it states 'one of the other' numbers, so for a = [1] it should be false

1 Comment
Assertion 2. is not correct. vector 'a' return with 6th element as 25 & thts a perfect square.. So it must be true!!
Problem Recent Solvers15062
Suggested Problems

1355 Solvers

Getting the indices from a matrix
572 Solvers

436 Solvers

806 Solvers

724 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!