Asked by Zach Adams
on 18 Oct 2019

Hi,

I need help finding the number of outputs a function has. This function finds pythagorean triples given a maxumum value of the hypotenuse. Addditionally, it negates all outputs that are not positive integers. The output of the code looks like:

Input the maximum length of a triangle: 10

Triangle:

Side A = 3

Side B = 4

Hypotenuse = 5

x = 3 %x is the value of Side A

Triangle:

Side A = 6

Side B = 8

Hypotenuse = 10

x = 6

I need to find how many x values the function has produced which should be 2 for this. I could also solve my problem by numbering the triangle by n+1 so the first triangle would be 1, the second 2, and so forth.

Thanks

Answer by Adam Danz
on 18 Oct 2019 at 16:53

Accepted Answer

@Zach Adams, first, more feedback. Please take some time to digest that. Second, I've improved your code without changing the algorithm and I've shown you how to determine the number of outputs.

Feedback on code

1) What is H and where does it come from? If H is supposed to be a variable, doesn't it cause an error? If H is a script or a function without any inputs, that needs improved for 2 reasons. a) "H" is a really bad function/file name. b) calling a script from a function defeats the advantages of functions - reproducibility and self-containment. If H is a function that merely asks for input you could move that code into his function. If H is a function that is used by other function, it's OK to keep it as it is but the name should really change. Functions should generally contain verbs or describe what it does. Example: getUserInput().

2) I see you have a variable named "max". You should avoid naming variables after common functions for two reasons. a) if you have a variable named max you will not be able to use the max() function anywhere in the same workspace and b) other people reading your code, including your future-self, will get confused because they are used to the "max" being a function. You can rename it to something like "maxC" if you'd like.

3) Use smart-indentation by selecting the entire code (ctrl + a) and then ctrl+i. I've corrected the code in your comment so that it has smart indentation. This makes it MUCH easier to read.

4) Your final for-loop is actually a conditional statement. Instead of "for hypotenuse > A &&..." use "if hypotenuse > A &&..."

5) 'M' is a very bad function name (same reasons as in point #1 above).

Cleaner version of your code

See inline comments.

function [A, B, hypotenuse] = findPythagTriplets(C)

%This calculates the pythagorean triples given the length of the hypotenuse.

% H; % H isn't even used anywhere

disp(1:1:C);

A = [];

B = [];

hypotenuse = [];

for maxC = 1:1:C

for m = round(sqrt(maxC/2)):round(sqrt(maxC))

% m == sqrt(maxC/2) && m == sqrt(maxC); %This line doesn't do anything at all

if m == floor(m)

n = sqrt(maxC - m^2);

end

A(end+1) = m^2 -n^2; %* The A(end+1) is generally bad practice but I didn't want to make major changes to your code

B(end+1) = 2*m*n; % this too

hypotenuse(end+1) = m^2 + n^2; %this too

if A(end) > B(end)

temp_A=A(end);

A(end) = B(end);

B(end) = temp_A;

end

if hypotenuse(end) > A(end) && hypotenuse(end) > B(end) && hypotenuse(end)^2 == B(end)^2+A(end)^2 && ...

A(end) >= 0 && A(end) == round(A(end)) && B(end) >= 0 && B(end) == round(B(end))

fprintf('Side A:')

disp(A(end))

fprintf('Side B:')

disp(B(end))

fprintf('Hypotenuse:')

disp(hypotenuse(end))

else

A(end) = [];

B(end) = [];

hypotenuse(end) = [];

end

end

end

Determine number of values in the output

To determine the number of triangles, just call the function with the output and count the number of values in the output variables.

[A, B, hypotenuse] = findPythagTriplets(10)

numberOfTriangles = numel(A)

% numberOfTriangles =

% 2

Zach Adams
on 18 Oct 2019 at 17:16

Adam Danz
on 18 Oct 2019 at 17:27

Thanks for the feedback! I was at the same stage several years ago so I know how ya feel.

Sign in to comment.

Answer by Sulaymon Eshkabilov
on 18 Oct 2019 at 15:22

Hi,

Here is your code improved a bit and bugs removed:

function [A, B, hypotenuse] = M(C)

%This calculates the pythagorean triples given the length of the

%hypotenuse.

disp([1:1:C]);

for max = 1:1:C

for m = round(sqrt(max/2)):round(sqrt(max))

m == sqrt(max/2) && m == sqrt(max);

if m == floor(m)

n = sqrt(max - m^2);

end

A = m^2 -n^2;

B = 2*m*n;

hypotenuse = m^2 + n^2;

if A > B

temp_A=A;

A = B;

B = temp_A;

end

if hypotenuse>A && hypotenuse > B && hypotenuse^2 == B^2+A^2 && A >= 0 && A == round(A) && B >= 0 && B == round(B)

fprintf('Side A = %f \n', A)

fprintf('Side B = %f \n', B)

fprintf('Hypotenuse = %f \n', hypotenuse)

end

end

end

Good luck.

Zach Adams
on 18 Oct 2019 at 16:36

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Adam Danz (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486025-find-the-number-of-outputs-of-a-value#comment_757631

## Zach Adams (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486025-find-the-number-of-outputs-of-a-value#comment_757639

## Zach Adams (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486025-find-the-number-of-outputs-of-a-value#comment_757640

## Adam Danz (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486025-find-the-number-of-outputs-of-a-value#comment_757646

## Zach Adams (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486025-find-the-number-of-outputs-of-a-value#comment_757649

## Adam Danz (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486025-find-the-number-of-outputs-of-a-value#comment_757651

## Zach Adams (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486025-find-the-number-of-outputs-of-a-value#comment_757872

Sign in to comment.