0

I was given the task to compute lcm of any two numbers.I have coded in python.The problem is when i compiled it under python2.7, i got a result which is different from , when i compiled under python3.

import sys

def gcd(a,b):     
   if b == 0:
     return a
   remainder = a % b
   return gcd(b,remainder)

def lcm(a, b):
  return int( (a*b) / gcd(a,b))

if __name__ == '__main__':
   input = sys.stdin.read()
   a, b = map(int, input.split())
   print(int(lcm(a, b)))

Input

226553150 1023473145

Output

46374212988031352 (under python3.5)

46374212988031350 (under python2.7)

Can someone help me ?

Community
  • 1
  • 1
James K J
  • 129
  • 1
  • 1
  • 12

1 Answers1

0

In Python 2 the division operator / will perform integer division, and not float division, when dealing with two integers. You can force the Python 3 behavior in Python 2 by importing division from __future__

>>> from __future__ import division
>>> 2/3
1.5
Bernardo Meurer
  • 2,295
  • 5
  • 31
  • 52