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...
Write a MIPS assembly language program to solve the following problem: For a set of integers...
Write a MIPS assembly language program to solve the following problem: For a set of integers stored in an array, calculate the sum of the even numbers and the sum of the odd numbers. The program should store these numbers in memory variables: evenSum and oddSum. Numbers should be read from the array one at a time with a zero value (0) being used to signal the end of data in the array.
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 using simple python programming language and make it easy to understand explain your code...
Please solve using simple python programming language and make it easy to understand explain your code as I am a beginner, use appropriate variable names which make the code easy to understand and edit if needed. A subsystem responsible for delivering priority numbers to an automated irrigation system has stopped working and you need to deliver a quick fix that will work until the actual subsystem is fixed by senior developer. As you are the newest addition to the development...
please use python to solve this problem. Math and String operations Write a script to perform...
please use python to solve this problem. Math and String operations Write a script to perform various basic math and string operations. Use some functions from Python's math module. Generate formatted output. Basic math and string operations Calculate and print the final value of each variable. a equals 3 to the power of 2.5 b equals 2 b equals b + 3 (use +=) c equals 12 c = c divided by 4 (use /=) d equals the remainder of...
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....
PYTHON PROBLEM: Goal: Design and implement a Python program to solve the following problem. Scientists measure...
PYTHON PROBLEM: Goal: Design and implement a Python program to solve the following problem. Scientists measure an object’s mass in kilograms and weight in newtons. If you know the amount of mass of an object in kilograms, you can calculate its weight in newtons with the following formula: [Note: Use or test any random numbers to check whether the weight is heavy, can be lifted or light] ????h? = ???? × 9.8 Write a program that asks the user to...
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...
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]...
5a Please code in language Ocaml In each of the three parts in this problem, you...
5a Please code in language Ocaml In each of the three parts in this problem, you will get full credit if you use foldT and define at most one helper function. (a) Define an OCaml function leafCount : ’a binTree -> int which returns an integer representing the total number of leaves in a tree. Starter Code: type 'a binTree = | Leaf | Node of 'a * ('a binTree) * ('a binTree) let leafCount (t : 'a binTree) :...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT