Problem 2024. Triangle sequence
A sequence of triangles is constructed in the following way:
- the first triangle is Pythagoras’ 3-4-5 triangle
- 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
- 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?
这么大串英文看着挺复杂,其实就下个三角形的最小的两条边相当于上个三角形的最大两条边,然后勾股就能知道斜边…刚开始运行没注意看最后几句话,求的是以斜边为正方形的面积…
l1 = 5;l2 = 4;l3 = 3;
for index = 1:n
if (index ~= n)
l3 = l2
l2 = l1
l1 = sqrt(l2^2+l3^2)
end
end
area = l1^2
然后刷评论区发现了这个链接
Calculating Fibonacci Numbers with Matrices and Linear Algebra
然后看了下别人的解法
大佬的写法就是这么简单且看不懂…
用了 regexp 匹配正则表达式函数
查了百度百科正则表达式,下次再(yi)学(ding)
百度百科
regexp '' '(?@ area = (8+17/sqrt(5))*((1+sqrt(5))/2)^n + (8-17/sqrt(5))*((1-sqrt(5))/2)^n);';
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.
判断是不是直角三角形
a = 5;b=12;c=13;
logic = (max([a,b,c])^2)-(min([a,b,c])^2)-(median([a,b,c])^2);
if logic == 0
flag = true
else
flag = false
end
评论区看到的 用 sort 也不戳 简单多了
a = 5;b=12;c=13;
x=[a b c];
y=sort(x);
flag = y(1)^2+y(2)^2==y(3)^2
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 .
给四个从小到大的数,判断里面能不能组成一个直角三角形
本来想着,敲了这么久代码不能写出那么多 if 的 low 代码
结果…
%Size: 89 Leading solution size is 8.
a = 5;b=12;c=13;d = 15;
num1 = [a,b,c];
num2 = [c,d];
count = 0;
index = 0;
for i = num1
for j = num2
if ismember(sqrt(j^2-i^2),num1)
count =count + 1;
index = index + 1;
break
end
if index == 1
break
end
end
end
if count~=0
flag = true
else
flag = false
end
别人写的两行搞定…
x = [a b c d].^2
flag = any(ismember(x,x'+x));
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.
略