1. Find Pythagorean Triplets
such that the 3rd number is within a Range
This code was created by me in order to find Pythagorean Triplets such that the Third number of the set lies in the given Range. Interestingly, multiples of 25 - 25, 50, etc are part of more than 1 triplet set and your code should be able to capture the same!
(I am taking the range as: 1100 to 1200)
% in MATLAB
% Show all the 3 numbers in the output
% Code created by Madhuresh Rai on Jan. 14th 2010
tic
lowerLimit = 1100 ; % Lower of the given range
upperLimit = 1200 ; % Higher of the given range
tempMat = lowerLimit:upperLimit ;
% Initialize the matrix with nos. from the given range
initRow = 1 ;
pythTriplets = zeros(10,3) ; % Initializing the answer matrix
for i = 1:1:upperLimit
for j = i:1:upperLimit % Use of 'i' should be noted!
if tempMat ( sqrt(i*i + j*j) == tempMat ) % Logically
pythTriplets(initRow,:) = [i j sqrt(i*i + j*j)] ;
initRow = initRow + 1 ;
end
end
end
disp('The required pair of pythagorean triplets are given below:')
pythTriplets
toc
No comments:
Post a Comment