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! :)