Given a vector of numbers, return true if one of the numbers is a square of one of the other numbers. Otherwise return false.
Example:
Input a = [2 3 4] Output b is true
Output is true since 2^2 is 4 and both 2 and 4 appear on the list.
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!!
great solution
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!
can you share the shortest code?
easy
Fun question!
the word "other" should be ignored in the description...
fun
Good question!
Cool
Clever problem
whoops, don't know how I managed to confuse .^ with .* for a bit there ---I was like, "What's WRONG?"
nice
Mnogo cod si napisal ;-)))))
probvai tova : b = ~isempty(intersect(a,a.^2))
Good question
0 and 1 elements in a will show the bug in your code
nice one-liner
a vector 'a' with elements 0 or 1 will show the bug in your code
out = any(ismember(a.*a, a))
cool!
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.
Lol
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?
How is the output true if the vector contains only one number? People posting such meaningless solutions: Please have some common sense!
easy easy
That's better...
a vector as v = [3,2,7,0,5] will show you your code's bug
a little smaller now
Nice ☺
Works
just kidding...
very easy
I want to make this code shorter!!
Case2,a = [20:30],fails.
Others pass.
Why???
in 2 test suite in a=[20:30] , contains 5^2 =25 so it has to be true not false
But there seems to be a better choice. Why forgetting ismember?
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
function example(n)
clc
y = n;
for i = 1:1:length(y)
for j = 1:1:length(y)
while y(i) == sqrt(y(j))
fprintf('true\n');
return
end
while y(i) ~= sqrt(y)
fprintf('false\n');
return
end
end
end
% my code runs successfully in matlab
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
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
Yeah, I did it
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.
Where did I do wrong？
The desired results are not the character strings 'true' and 'false' but the logical values true and false.
hint ; any and ismember.
submitted tested solution but still showing error......
L 17 (C 1-3): Parse error at END: usage might be invalid MATLAB syntax.
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 ...
This is the correctly executing in matlab
I wonder...if you cheat, why even bother to actually solve the problem?
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 :)
Code works on Matlab, but not on your test suite?
Srtr2func, classic way:)a new thing learned.
test case 3 should be 'false'
it states 'one of the other' numbers, so for a = [1] it should be false
Assertion 2. is not correct. vector 'a' return with 6th element as 25 & thts a perfect square.. So it must be true!!