Question

I need help figuring this out and coding it into Pycharm (Python). Two integers are relatively...

I need help figuring this out and coding it into Pycharm (Python). Two integers are relatively prime if they have no common divisors other than 1. An equation in the form ax + by = 1 has an integer solution if and only if a and b are relatively prime. For example, if a = 8 and b = 21, then integer solutions exist for x and y in the equation 8x + 21y = 1. The solutions can be found by reducing the coefficients and back-substituting.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
# function to return the factor list of a number
def fact(num):
   factors = []
   for i in range(1, num+1):
      if num % i == 0:   # if factor
         factors.append(i)  # add to list
   return factors # return list

# function to determine the if the solution for an equation exists or not
def solution(a, b):
   if a > b and a % b == 0:   # if b is factor of a
      return False
   elif a < b and b % a == 0: # if a is factor of b
      return False
   elif a == b:   # if both are equal mean they are not coprime
      return False
   else: 
      # get the factor lists of both a and b
      factors_a = fact(a)    
      factors_b = fact(b)

      # check if any factor is common or not
      for i in factors_a:
         for j in factors_b:
            if i == j and i != 1:  # if yes then no solution
               return False

      # solution exist if function reaches here
      return True    

# main function to drive the code
def main():

   # read input for a and b
   a = int(input('Enter a: '))
   b = int(input('Enter b: '))

   # create an equation string
   eqn = '{}x + {}y = 1'.format(a, b)

   # if solution exists
   if solution(a, b):
      sols = []
      print('The solution for equation {} exist.'.format(eqn))

      # calculate the solutions in range(-100 to 100) you can modify this range
      for x in range(-100, 100):
         for y in range(-100, 100):
            if (a*x + b*y) == 1:
               sols.append((x, y))    # append solutions to list

      # display solutions
      print('Solutions are:\n {}'.format(sols))
   
   # solution do not exist
   else:
      print('The solution for equation {} does not exist.'.format(eqn))

# call main function
if __name__ == "__main__" : main()

FOR HELP PLEASE COMMENT.
THANK YOU.

Add a comment
Know the answer?
Add Answer to:
I need help figuring this out and coding it into Pycharm (Python). Two integers are relatively...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT