how do i write a program that calculate side length c using pythagoras' theorem(a2+b2=c2) assuming a and b to be integer values starting from 1, and print out the pythagorean results that satisfy a>=1,b>=1 and c<=10.

66 ビュー (過去 30 日間)
how do i write a program that calculate side length c using pythagoras' theorem(a2+b2=c2) assuming a and b to be integer values starting from 1, and print out the pythagorean results that satisfy a>=1,b>=1 and c<=10. And also print the number of combinations that satisfy the above restrictions and the sum of all b values of the valid combinations.
Is there any wrong with my code?
  3 件のコメント
Zainab Ali
Zainab Ali 2020 年 8 月 16 日
Write a user-defined function for Pythagoras Theorem to find the Hypotenuse ‘c’ where c2 = a2 + b2.
John D'Errico
John D'Errico 2020 年 8 月 16 日
@Zainab Ali - please do not post answers to a question that are themselves questions. You posted two such answers, after having made this comment that is in itself just a homework assignment. As homework, with no effort made, it is yours to do.

サインインしてコメントする。

回答 (2 件)

KSSV
KSSV 2018 年 4 月 18 日
編集済み: KSSV 2018 年 4 月 18 日
a = 1:0.1:20 ;
b = 1:0.1:20 ;
[A,B] = meshgrid(a,b) ;
C = A.^2+B.^2 ;
C2 = C.^2 ;
% 
idx = C2<=10 ;
iwant = [A(idx) B(idx) C2(idx)]

Sara Boznik
Sara Boznik 2020 年 8 月 16 日
n=0;
sum=0;
for a=1:1:20
for b=1:1:20
c=sqrt(a.^2+b.^2);
if c<=10
fprintf('c=%.2f,a=%d,b=%d\n',c,a,b)
n=n+1;
sum=sum+b;
end
end
end
n
sum
Here you have code with n which tell you number of combinations that satisfy the above restrictions. And sum of all b values of the valid combinations tell you sum.

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by