Effacer les filtres
Effacer les filtres

i tried to find if there are perfect numbers in the code stating that 1 should be square of other in the same matrix or array

2 vues (au cours des 30 derniers jours)
function b = Squared(a) n=length(a) for i=1:n for j=i:n if a(i)*a(i)==a(j) b=true break else b=false end end

Réponse acceptée

Swaroopa
Swaroopa le 28 Août 2022
Modifié(e) : Swaroopa le 28 Août 2022
Hi Hannah,
You can sort the array or matrix and then use the same code.
You can also use indentation rules for better clarity.
function output = squared(arr)
n=length(arr)
output=false
for i=1:n
for j=i:n
if arr(i)*arr(i)==arr(j)
output=true
break
else
output=false
end
end
end
end
Hope it helps

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by