2

I was making a simple calculator:

import math

number1 = input("Enter a number: ")
number2 = input("Enter a number: ")

result = math.lcm(int(number1), int(number2))

print(result)

when I got the error in the title. This works in the shell and even code as simple as

import math
math.lcm(10, 20)

gives me the error.

pepo_boyii
  • 60
  • 1
  • 7
  • See also: [this older question asking about `lcm` in python](https://stackoverflow.com/questions/51716916/built-in-module-to-calculate-the-least-common-multiple) – Stef Nov 23 '20 at 23:33
  • Any chance you've defined your own module `math` that is taking precedence over the standard one? Search for `math.py`. – Mark Ransom Nov 26 '20 at 04:42

2 Answers2

4

Well, let's take a look at the documentation for math.lcm to see why it sometimes exists and sometimes doesn't exist!

What's it says? New in version 3.9.

Looks like your code works when run with python 3.9, and doesn't work when run with python 3.8 or older.

Quick fix, python 3.5 to 3.8

On the other hand, math.gcd exists since version 3.5. If you need an lcm function in python 3.5, 3.6, 3.7 or 3.8, you can write it this way:

import math

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

Quick fix, python < 3.5

So lcm is in math since 3.9, and gcd is in math since 3.5. What if your python version is even older than that?

In older versions, gcd wasn't in math, but it was in fractions. So this should work:

import fractions

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

Which python version am I using?

Please let me refer you to the kind user who asked that exact question:

Stef
  • 13,242
  • 2
  • 17
  • 28
  • @pepo_boyii Please read the text I wrote in my answer and not just the code. – Stef Nov 23 '20 at 23:24
  • @pepo_boyii You had trouble with `math.lcm` because it only exists since 3.9. One of the two pythons versions you're using is too old and doesn't have `math.lcm`. Now you have trouble with `math.gcd`. This function exists since python 3.5. I am guessing you are using a python version older than 3.5. – Stef Nov 23 '20 at 23:25
  • My interpreter shows I'm using 3.9. Maybe there's something wrong with the interpreter? – pepo_boyii Nov 23 '20 at 23:36
  • @pepo_boyii print the Python version *in the script you're running* to make sure you don't have different versions installed on your system. – Mark Ransom Nov 23 '20 at 23:41
  • @MarkRansom still gives me 3.9.0 :/ Something is obviously wrong here but I just can't find what it is – pepo_boyii Nov 23 '20 at 23:45
  • 1
    @pepo_boyii -As Stef said, "math.lcm()" is a new function supported by Python3.9. It is recommended that you use this function when using the Python3.9 [interpreter](https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment). – Jill Cheng Nov 25 '20 at 05:15
0

lcm() can have more than two arguments, like in Python 3.9+:

from math import gcd

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

def lcm(*integers):
    a = integers[0]
    for b in integers[1:]:
        a = (a * b) // gcd (a, b)
    return a