Question

In: Computer Science

Please solve this problem with Python Language. P#2. A Pythagorean triplet is a set of three...

Please solve this problem with Python Language.

P#2. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which a2 + b2 = c2 . For example, 32 + 42 = 52. And a + b + c = 3 + 4 + 5 = 12. There exists exactly two Pythagorean triplet for which a + b + c = 300. Find a, b, c.

Hints: Level-0 Algorithm:

1. Find the possible ranges of a and b for which a+b+c is around 300 (include the paper & pencil analysis at the end of your report)

2. Compute c for each combination of a and b

3. Verify if ?+?+?=300 and ?2+?2=?2 and ?<?<?

4. Print results

I really don't understand to do this problem.

Solutions

Expert Solution

PROGRAM:
import math


def pythagorean_triplet(n):     #defining function to find all possible triplet
    for b in range(2,n):        #starting value of b with 2 
        for a in range(1, b):   #staring value of a with 1 so a<b
            c = math.sqrt(a * a + b * b)   #find the value of c=(a^2+b^2)^(1/2)
            if c % 1 == 0 and a+b+c==n and a<b<c:  #verify three conditions 
                print(a, b, int(c))


pythagorean_triplet(300)

PROGRAM SCREENSHOT:


OUTPUT OF PROGRAM:

Related Solutions

A Pythagorean triplet is a set of positive integers (x, y, z) such that x2 +...
A Pythagorean triplet is a set of positive integers (x, y, z) such that x2 + y2 = z2. Write an interactive script that asks the user for three positive integers (x, y, z, in that order). If the three numbers form a Pythagorean triplet the script should a) display the message ‘The three numbers x, y, z, form a Pythagorean triplet’ b) plot the corresponding triangle using red lines connecting the triangle edges. Hint: place the x value on...
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
please, solve this problem.: implementing with java language create a bank account management system according to...
please, solve this problem.: implementing with java language create a bank account management system according to the following specifications: BankAccount Abstract Class contains the following constructors and methods: BankAccount(name, balance): a constructor that creates a new account with a name and starting balance. getBalance(): a method that returns the balance of a specific account. abstract deposit(amount): abstract method to be implemented in both Checking and SavingAccount classes. abstract withdraw(amount): abstract method to be implemented in both Checking and SavingAccount classes....
IN PYTHON Write a program to do the following: Solve the a set of equations as...
IN PYTHON Write a program to do the following: Solve the a set of equations as mentioned in "Zybook 5.20 LAB: Brute force equation solver". Instead of the arithmetic operators use your own function defined in a module named calc. You must provide two files (calc.py, brute_force_solver.py) :- 1) calc.py:- Add function named 'add' instead of using operator '+' [10pts] Add function named 'difference' instead of using operator '-' [10pts] Add function named 'product' instead of using operator '*' [10pts]...
note (Please solve the questions in the language of operating systems) [2] (i) Define Timesharing.    ...
note (Please solve the questions in the language of operating systems) [2] (i) Define Timesharing.     (ii) In a one processor system, there is an interrupt clock which is set to a “time slice Q”, that is, every Q an interrupt occurs to stop the process. There is only one I/O device in the system    which is interrupted when the process needs an I/O. There are 3 processes A, B, C.           Process A executes an I/O interrupt every...
Solve the following question by using python language In range of 1-10000 if the numbers are...
Solve the following question by using python language In range of 1-10000 if the numbers are divisible by n1 increase count by 1 , if the number is divisible by n2 increase count by 2, and if the number are divisible by n3 increase the count by 3 if none of the above conditions match for the number, increase count by the number. n1=10 +17 n2=50 +21 n3=100 +9
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
USE PYTHON TO SOLVE THIS PROBLEM A bacteria culture grows exponentially. After 2 hours, the bacteria...
USE PYTHON TO SOLVE THIS PROBLEM A bacteria culture grows exponentially. After 2 hours, the bacteria count was 400 cells and after 6 hours, the bacteria count was 20,000 cells. a) Solve a system of equations to find (approximately) k and y0 (HINT: If will help to assume K is real in the symbols command) b) Use this to determine when the bacteria count reaches 2,000,000 (exact and approximate). c) Suppose 400 was the “initial” amount and 20,000 the count...
USE PYTHON TO SOLVE THIS PROBLEM A bacteria culture grows exponentially. After 2 hours, the bacteria...
USE PYTHON TO SOLVE THIS PROBLEM A bacteria culture grows exponentially. After 2 hours, the bacteria count was 400 cells and after 6 hours, the bacteria count was 10,000 cells. a) Solve a system of equations to find (approximately) k and y0 (HINT: If will help to assume K is real in the symbols command) b) Use this to determine when the bacteria count reaches 1,000,000 (exact and approximate). c) Suppose 400 was the “initial” amount and 10,000 the count...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT