Question
Computer Science - Coding in Python Converting a decimal to a fraction Although computers generally represent numbers as decimals, it is often convenient to express
Computer Science - Coding in Python
Converting a decimal to a fraction
Although computers generally represent numbers as decimals, it is often convenient to express values as fractions. If we desire to convert a decimal to a fraction, we need to follow a two-step process:
Starting with the number itself, multiply by ten repeatedly until no values remain after the decimal point. This is the numerator. Keep the multiplier as the denominator.
Simplify the fraction.
For instance, for 0.75:
Multiply by ten repeatedly:
0.751010=750.751010=75
Thus the numerator is 75 and the denominator is 1010=1001010=100.
Simplify the fraction:
Find the factors of the numerator and the denominator.
75?{1,3,5,25,75}100?{1,2,4,5,10,20,25,50,100}75?{1,3,5,25,75}100?{1,2,4,5,10,20,25,50,100}
Find the greatest common factor of each and divide both by it.
7525=310025=47525=310025=4
0.75?340.75?34
Write a function factors( n ) which accepts a number n and returns a list containing all of the factors of that number. (This is a common task. Write your own codedon't just google it!)
Write a function fraction( n ) which accepts a decimal number n and returns a tuple ( numerator,denominator ) of the resulting fraction representation.
Your submission should include two functions factors( n ) and fraction( n ).
STARTER CODE:
def factors( n ): pass
def fraction( n ): n_str = str( n ) decimal_part = n_str[ n_str.find( '.' )+1: ] # 1. Multiply by ten repeatedly (to make all of decimal positive). numer = n * 10 ** ??? denom = ???
# 2. Find factors. numer_factors = ??? denom_factors = ??? factor = 1 # ??? find greatest common factor of both numerator and denominator # ??? divide both by GCF before returning them return ( numer,denom )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started