Problem 23. Finding Perfect Squares
Solution Stats
Problem Comments
-
39 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 for-loops, 1 function, 1 if-condition.
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
@Max Weinberg you can't use else because test1 will report an error
function b = isItSquared(a)
b = false;
for i = 1:length(a)
if any(a == sqrt(a(i))^2)
b = true;
return;
end
end
end
can sbd tell me why this code wrong at case 3, 7and 8
@Tran Up to floating point arithmetic, sqrt(a(i))^2 is the same as a(i), so you're really just testing whether the vector a is non-empty.
(Note that the failing cases are the one where the correct answer is "false", while the ones where you succeed are the ones where it is "true").
Nice little problem :)
It was very interesting
Solution Comments
Show commentsProblem Recent Solvers18574
Suggested Problems
-
Return the largest number that is adjacent to a zero
5336 Solvers
-
5894 Solvers
-
Matlab Basics - Absolute Value
607 Solvers
-
666 Solvers
-
7164 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!