Question

PLEASE HELP! python codeProblem 4. Define the function pythagorian_coprimes (n=100) that prints all pairs of positive integer numbers (a, b) such tha

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

Note: Please read the comments attached very carefully, because there could be different variations of the method (especially pythagorian_coprimes) according to different translations of the question, and if the code I attached did not perform what you expect it to perform, let me know first, I’ll update the code quickly.

#code

# problem 4
def pythagorian_coprimes(n=100):
    for a in range(2, n):
        for b in range(2, a):
            c = (a * a + b * b) ** 0.5
            if c > n:
                break
            if c == round(c):
                # storing values of a and b in two variables, to find gcd
                # note: if you are allowed to use math library, you can
                # just simply call gcd=math.gcd(a,b) and remove below 5 lines (not comments)
                x = a
                y = b
                # looping until y is 0
                while y > 0:
                    # using euclidian algorithm to find gcd of x and y
                    x, y = y, x % y
                # at the end, value of x will be the gcd
                gcd = x
                # checking if gcd is 1
                if gcd == 1:
                    # printing a and b. if you want to yield values instead, change print to yield
                    # you may also include c if you want, but the question demands "prints all pairs
                    # of positive integer numbers (a,b)" which is why I wrote the below line.
                    print (a, b)


# problem 5
def acronym(string):
    # creating an empty string
    s = ''
    # looping through each word in string
    for word in string.split():
        # converting first letter to upper case and appending to s
        s += word[0].upper()
    # returning s
    return s


# testing problem4
# note: if you used yield instead of print inside pythagorian_coprimes, you will need to use a loop
# like -> for a,b in pythagorian_coprimes(): print(a,b)
pythagorian_coprimes()

# similarly call acronym method with test input if you want to.

#code screenshot

1 2 3 4 5 6 # problem 4 def pythagorian_coprimes (n=100): for a in range(2, n): for b in range(2, a): C = (a * a + b* b) ** 0

#output

43 125 158 21 28 247 35 12 48 9 45 28 55 488 63 68 11 316 72 65 77 36 8日 9 84 13

Add a comment
Know the answer?
Add Answer to:
PLEASE HELP! python code Problem 4. Define the function pythagorian_coprimes (n=100) that prints all pairs of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • please use python to solve Problem 4 (a) Write a function is_sum_squares (n) that returns True...

    please use python to solve Problem 4 (a) Write a function is_sum_squares (n) that returns True if n can be written as a² + b2 , where a, b are integers, and False otherwise. For example is_sum_squares (5) is True since 5 = 12 + 22, while is_sum_squares (3) is False. (b) Compute is_sum_squares (7), is_sum_squares (11), is_sum_squares (13), is_sum_squares(17), is_sum_squares (19), is_sum_squares (23). (c) What is the pattern for is_sum_squares(p) when p is prime. (HINT: look at p %...

  • i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transf...

    i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transformation of a sequence (or collection) of coloured disks. Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, cl. For example, the colour mapping might be: O-red, 1-yellow, 2-blue, 3-pink,. c-black For a given sequence of coloured disks eg.,0,1,2,3,4), at each time step (or iteration) you are only...

  • Python, given a code in class i just need help with the third bullet point ; using a and n (defin...

    Python, given a code in class i just need help with the third bullet point ; using a and n (defined in the second picture of python code) find the first digit for k! for k =1,...,n. you dont have to type in all this code just help me write the code in the first picture where it says: def benford(a): b = [0 for d in range(1,10)] #Do everthything in here return b 2.2 Generating data In this assignment...

  • Please write the complete code in C. Write a C function that prints the minimum spanning...

    Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...

  • please help! SHOW ALL WORK Can someone explain why for part b when I solve for the wavelength instead of 0.28 i get 2.81(im doing 2pi/k and have no clue why the answer is 0.28) Name Problem 4 Two...

    please help! SHOW ALL WORK Can someone explain why for part b when I solve for the wavelength instead of 0.28 i get 2.81(im doing 2pi/k and have no clue why the answer is 0.28) Name Problem 4 Two waves, one with wave function D0.250 sin(2.27x 16.71), and another with wave function D2 = 0.250 sin(2.27x + 16.70, interfere to form a wave with displacement D = Di D2-Note that aff values are in SI (mks) units and angles are...

  • Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write...

    Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...

  • please help answer these QUESTION 4 Which managerial function involves setting goals, establishing a strategy to...

    please help answer these QUESTION 4 Which managerial function involves setting goals, establishing a strategy to pursue those goals, and forecasting future threats and opporturites hat might influence the company's needs and strategies? O A planning С.leading ○D.cortrolig QUESTIONS What term reders to the fit between an individual's values, beliefs, attitudes, and personality and the values, norms, and cuiture of the organization? OA person-organization f ○B.person+ob fit O C.person-vocation ft OD.person-group fit QUESTION 6 What term refers to the fit...

  • ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE...

    ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 // READ BEFORE YOU START: // Please read the given Word document for the project description with an illustrartive diagram. // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, standard, and a linked list of absents. // Please read the instructions...

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT