- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Determine all Pythagorean Triplets in the Range
When it is required to determine the Pythagorean triplets within a given range, a method is defined, that helps calculate the triplet values.
Below is the demonstration of the same −
Example
def pythagorean_triplets(limits) : c, m = 0, 2 while c < limits : for n in range(1, m) : a = m * m - n * n b = 2 * m * n c = m * m + n * n if c > limits : break print(a, b, c) m = m + 1 upper_limit = 15 print("The upper limit is :") print(upper_limit) print("The Pythagorean triplets are :") pythagorean_triplets(upper_limit)
Output
The upper limit is : 15 The Pythagorean triplets are : 3 4 5 8 6 10 5 12 13
Explanation
A method is defined that defines variables to define values for each of the Pythagorean triplet.
Outside the method, the integer is defined.
This method is called by passing the integer.
The output is displayed on the console.
Advertisements