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:...
(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
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.
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 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...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
CODE IN PYTHON 1. Write a program that asks the user for the number of slices...
CODE IN PYTHON 1. Write a program that asks the user for the number of slices of pizza they want to order and displays the total number of dollar bills they owe given pizza costs 3 dollars a slice.  Note: You may print the value an integer value. 2. Assume that y, a and b have already been defined, display the value of x: x =   ya+b    3. The variable start_tees refers to the number of UD T-shirts at the start...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT