0

from the user, we are required to ask for (1) the number of test cases, and (2) two integers that make up the fraction (i did it as a:numerator and b:denominator). then we are required to print that fraction in this form (1/2 + 1/y). also, we have to use the gcd function in writing our code

we were given a couple of sample inputs to check our work:
- input 2 and 3 must output '1/2+1/6'
- input 1 and 4 must output '1/2+1/-4'
- input 7 and 10 must output '1/2+1/5'
- input 1 and 1 must output '1/2+1/2'

here is my code:

N = int(input())
index = 1

while index<=N:
  a = int(input())
  b = int(input())
  num = 2*a -b
  den = 2*b
  def gcd(x,y): #this calculates for the greatest common divisor
    if x>y:
      smaller=y
    else:
      smaller=x 
    for factor in range(1,smaller+1):
      if (x%factor==0) and (y%factor==0):
        hcf=factor #this is the gcd
        newNum=num//hcf
        newDen=den//hcf
        newFrac=str(newNum)+'/'+str(newDen) #to simplify the fraction, both numerator and denominator are divided by the gcd
        print('1/2+'+newFrac)
  index = index + 1
  gcd(num,den)

when trying to run the code, there are some inputs that give me the result I want, but there are some that don't...what's wrong with my code? should I put a broken line somewhere or something?

sample input # and what they output (in comments)
4 # number of test cases
2
3
#outputs 1/2+1/6 ---> correct
1
4
#outputs nothing ---> incorrect
7
10
#outputs 1/2+4/20
#        1/2+2/10
#        1/2+1/5 ---> outputs too much ---> incorrect
1
1
#outputs 1/2+1/2 ---> correct

thanks in advance! :)

user8638151
  • 21
  • 1
  • 7
  • Possible duplicate of [How to convert a decimal number into fraction?](https://stackoverflow.com/questions/23344185/how-to-convert-a-decimal-number-into-fraction) – ZdaR Oct 23 '17 at 05:49

1 Answers1

1

You can try this:

N = int(input())

for __ in range(N):

    a = int(input())
    b = int(input())

    a_b_str = ('%i/%i' %(a,b)).ljust(5) # for printout purposes; optional

    num = 2*a - b
    den = 2*b

    if num == 0:
        print(a_b_str + ' -->  1/2')

    elif den % num == 0:
        y = den / num
        print(a_b_str + ' -->  1/2+1/%i' %y)

    else:
        print(a_b_str + ' CAN NOT BE REPRESENTED IN THE FORM: "1/2+1/y"') 

Results: (for the respective a,b)

2/3   -->  1/2+1/6
1/4   -->  1/2+1/-4
7/10  -->  1/2+1/5
1/1   -->  1/2+1/2

1/2   -->  1/2
1/5   CAN NOT BE REPRESENTED IN THE FORM: "1/2+1/y"

3/2   -->  1/2+1/1
5/2   CAN NOT BE REPRESENTED IN THE FORM: "1/2+1/y"

-1/2  -->  1/2+1/-1
-1/4  CAN NOT BE REPRESENTED IN THE FORM: "1/2+1/y"

Actually, you don't need to do much stuff for your task since:

a/b = 1/2 + 1/y 
=> 1/y = a/b - 1/2
=> 1/y = (2*a - b) / 2*b
=> y = 2*b / (2*a - b) = den / num

UPD.

The alternative solution - with gcd():

# gcd based on Euclid's algorithm
def gcd(b, a): 
    while a:
        b, a = a, b%a
    return abs(b)

N = int(input())

for __ in range(N):

    a = int(input())
    b = int(input())

    a_b_str = ('%i/%i' %(a,b)).ljust(5) 

    num = 2*a - b
    den = 2*b

    if num == 0:
        print(a_b_str + ' -->  1/2')

    else:
        gcd_temp = gcd(den, num)

        if gcd_temp == abs(num):
            y = (num/gcd_temp) * (den/gcd_temp)
            print(a_b_str + ' -->  1/2+1/%i' %y) 

        else:
            print(a_b_str + ' CAN NOT BE REPRESENTED IN THE FORM: "1/2+1/y"')
MaximTitarenko
  • 886
  • 4
  • 8