# Matlab Cup Challenge Cody Solution

Learn Matlab and earn a badge and skill. This section contains the Cody solution of MATLAB Cup Challenge Cody Solution. Total 18 solved problems.

Visit the Matlab official Problems page of Matlab Cup Challenge.

All solutions were verified and working. It may be possible after a year or more community may modify the question, then the provided solution may not work so do comment below about the same. Our team will suddenly work on it.

Get Notified Faster

## Problem 1974. Length of a short side

Calculate the length of the short side, a, of a right-angled triangle with hypotenuse of length c, and other short side of length b.

Given the hypotenuse length c of a right-angled triangle and the length b of one short side, find the length a of the other short side. Use Pythagorean Law directly:

function a = calculate_short_side(b, c)
a = sqrt(c^2-b^2);
end

## Problem 2020. Area of an Isoceles Triangle

An isosceles triangle has equal sides of length x and a base of length y. Find the area, A, of the triangle.

function A = isocelesArea(x,y)
A = sqrt(x^2-(y/2)^2)*y*0.5
end

## Problem 2018. Side of a rhombus

If a rhombus has diagonals of length x and x+1, then what is the length of its side, y?

If the two diagonals of a rhombus are x and x+1 respectively, find the side length y of the rhombus.

function y = rhombus_side(x)
y = sqrt((x/2)^2+((x+1)/2)^2);
end

## Problem 2017. Side of an equilateral triangle

If an equilateral triangle has area A, then what is the length of each of its sides, x?

The area of ​​an equilateral triangle is A, find the side length x. According to the area formula of an equilateral triangle
so

function x = side_length(A)
x = sqrt(4*A/sqrt(3));
end

## Problem 2016. Area of ​​an equilateral triangle

Calculate the area of an equilateral triangle of side x.

Calculating the area of ​​an equilateral triangle with side length x is the inverse problem of the third problem.

function y = equilateral_area(x)
y = sqrt(3)/4*x*x;
end

## Problem 2015. Length of the hypotenuse

Given short sides of lengths a and b, calculate the length c of the hypotenuse of the right-angled triangle.

Given the short sides a and b of a right triangle, find the hypotenuse c. Also appeared in the question group Introduction to MATLAB

function c = hypotenuse(a,b)
c = sqrt(a^2+b^2);
end

## Problem 1974. Length of a short side

Calculate the length of the short side, a, of a right-angled triangle with hypotenuse of length c, and other short side of length b.

Given the hypotenuse length c of a right-angled triangle and the length b of one short side, find the length a of the other short side. Use Pythagorean Law directly:

function a = calculate_short_side(b, c)
a = sqrt(c^2-b^2);
end

## Problem 2024. Triangle sequence

This question can be regarded as one of the more interesting questions in this question, but the description of the question is very long:

A sequence of triangles is constructed in the following way:

1) the first triangle is Pythagoras’ 3-4-5 triangle

2) the second triangle is a right-angle triangle whose second longest side is the hypotenuse of the first triangle, and whose shortest side is the same length as the second longest side of the first triangle

3) the third triangle is a right-angle triangle whose second longest side is the hypotenuse of the second triangle, and whose shortest side is the same length as the second longest side of the second triangle etc.

Each triangle in the sequence is constructed so that its second longest side is the hypotenuse of the previous triangle and its shortest side is the same length as the second longest side of the previous triangle.

What is the area of a square whose side is the hypotenuse of the nth triangle in the sequence?

function area = triangle_sequence(n)
a(1)=9;
a(2)=16;
for i=1:n
a(i+2)=a(i+1)+a(i);
end
area = a(n+2);
end

## Problem 2023. Is this triangle right-angled?

Given any three positive numbers a, b, c, return true if the triangle with sides a, b and c is right-angled. Otherwise, return false.

Given any side lengths a, b, and c, judge whether the formed triangle is a right-angled triangle. Note that c is not a hypotenuse.

function flag = isRightAngled(a,b,c)
abc=[a,b,c];
max_abc=max(abc);
flag =(max_abc)^2==sum(abc(abc<max_abc).^2);
end

## Problem 2022. Find a Pythagorean triple

Given four different positive numbers, a, b, c and d, provided in increasing order: a < b < c < d, find if any three of them comprise sides of a right-angled triangle. Return true if they do, otherwise return false .

Given four numbers, abcd, satisfying a<b<c<d, judge whether any three numbers can form a right triangle.

function flag = isRightAngled(a, b, c)
flag = c^2==a^2+b^2;
end

## Problem 2021. Is this triangle right-angled?

An isosceles triangle has equal sides of length x and a base of length y. Find the area, A, of the triangle.

Given the waist length x and base y of the isosceles triangle, find the area.

First find the height, the height and the bottom edge are perpendicular to the midpoint of the bottom edge, so the height is calculated according to the waist length and half of the bottom edge:

Then the area is equal to the base times the height divided by 2.

function flag = isRightAngled(a, b, c)
flag = c^2==a^2+b^2;
end

## Problem 2019. Dimensions of a rectangle

The longer side of a rectangle is three times the length of the shorter side. If the length of the diagonal is x, find the width and length of the rectangle.

Knowing that the long side is three times the short side, and the diagonal length is x, find the long side (length) and short side (width).

Suppose the width (short side) is y, then the length is 3y. According to the Pythagorean theorem, 10y^2=x^2, so y=sqrt(0.1)x and the length is 3*y.

function [width, length] = findRectangleDimensions(x)
width = sqrt(0.1)*x;
length = 3*width;
end

## Problem 2020. Area of an Isoceles Triangle

An isosceles triangle has equal sides of length x and a base of length y. Find the area, A, of the triangle.

function A = isocelesArea(x,y)
A = sqrt(x^2-(y/2)^2)*y*0.5
end

## Problem 2018. Side of a rhombus

If a rhombus has diagonals of length x and x+1, then what is the length of its side, y?

If the two diagonals of a rhombus are x and x+1 respectively, find the side length y of the rhombus.

function y = rhombus_side(x)
y = sqrt((x/2)^2+((x+1)/2)^2);
end

## Problem 2017. Side of an equilateral triangle

If an equilateral triangle has area A, then what is the length of each of its sides, x?

The area of ​​an equilateral triangle is A, find the side length x. According to the area formula of an equilateral triangle
so

function x = side_length(A)
x = sqrt(4*A/sqrt(3));
end

## Problem 2016. Area of ​​an equilateral triangle

Calculate the area of an equilateral triangle of side x.

Calculating the area of ​​an equilateral triangle with side length x is the inverse problem of the third problem.

function y = equilateral_area(x)
y = sqrt(3)/4*x*x;
end

## Problem 2015. Length of the hypotenuse

Given short sides of lengths a and b, calculate the length c of the hypotenuse of the right-angled triangle.

Given the short sides a and b of a right triangle, find the hypotenuse c. Also appeared in the question group Introduction to MATLAB

function c = hypotenuse(a,b)
c = sqrt(a^2+b^2);
end

## Problem 2022. Find a Pythagorean triple

Given four different positive numbers, a, b, c and d, provided in increasing order: a < b < c < d, find if any three of them comprise sides of a right-angled triangle. Return true if they do, otherwise return false .

Given four numbers, abcd, satisfying a<b<c<d, judge whether any three numbers can form a right triangle.

function flag = isRightAngled(a, b, c)
flag = c^2==a^2+b^2;
end

Matlab Cup Challenge Cody 18 solved problems. All solution is correct as they were first submitted in Matlab and then uploaded here for your help. If any solution doesn’t work then do comment.

Also Check the solution of other Matlab Cody Contest Matlab Cody More Contest solutions.