Question

In: Computer Science

Write a Python 3 code that takes an irrational number 'x' and return 'm' fraction approximations....

Write a Python 3 code that takes an irrational number 'x' and return 'm' fraction approximations. For example let x=pi and m=4, then the program should return 4 examples of pi written as a fraction approximation.  

Solutions

Expert Solution

The code which takes an irrational number x and returns the given number of fraction approximations is given below and will be explained below -

from fractions import Fraction
import math

x = float(input('Enter irrational number: '))
m = int(input('Enter number of fraction approximations: '))

c=10

for i in range(1,m+1):
    res = Fraction(x).limit_denominator(int(math.pow(c,i)))
    print(res)

Explanation -

  • Import the fraction and math libraries.
  • Take x and m as inputs. x will be a float as it is a decimal and m will be an integer.
  • Initialize a variable to 10.
  • Now, we run a loop from 1 to m, i.e if m=4, the loop will run from 1,2,3, and 4.
  • The line Fraction(x) converts a decimal to a fraction. But it can give very large fractions. So we use limit_denominator so that denominator is set to a small value. The value which we have chosen is 10,100,1000 and so on. To calculate that we use math.pow function.
  • We print the fraction value at each step.

Output -

As it is seen in the output screenshot, the entered irrational number is pi(3.141592) and I want 4 function approximations. Now if you see the output, we have the denominators limited to 10,100,1000 and so on. So the first approximation comes out to be 22/7 which is expected.


Related Solutions

Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the product of two prime numbers ? and ? and returns the tuple ( ?, ? ) where ?≤? . Example: ????????(22)=(2,11) Example: ????????(3605282209)=(59447,60647) This problem has a time-out limit of 1 second and a memory limit of 1MB. The number ? in all test-cases will satisfy 4≤?≤800000000000000 For example: Test Result print(factorMe(22)) (2, 11) print(factorMe(3605282209)) (59447, 60647)
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
Write the code to return the output in Rstudio. What is the code? Code: x <-...
Write the code to return the output in Rstudio. What is the code? Code: x <- c(28, 69, 5, 88, 19, 20) Output must be: [1] 4 2 1 6 5 3
Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
In python of Jupiter notebook Write a python function called trng that takes three numbers x,...
In python of Jupiter notebook Write a python function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangleotherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT