I'm writing a function that will determine if a given number if "ugly". It's ugly if the only prime factors it has are 2, 3, and 5 (it can have less than all 3 but no others).
I get an error, saying: "Line 22: NameError: global name 'getPrimeFactors' is not defined".
What am I doing incorrectly?
import math
class Solution(object):
def isPrime(self, num):
for i in range(2, math.floor(math.sqrt(num))):
if num % i == 0:
return false
return true
def getPrimeFactors(self, num):
primeFactors = []
for i in range(2, math.floor(math.sqrt(num))):
if isPrime(i) and num % i == 0:
primeFactors.append(i)
return primeFactors
def isUgly(self, num):
"""
:type num: int
:rtype: bool
"""
primeFactors = getPrimeFactors(num)
for factor in primeFactors:
if factor != 2 and factor != 3 and factor != 5:
return false
return true
Thanks!
Use getPrimeFactors
as self.getPrimeFactors(num)
because it's defined as a method inside the class Solution
:
primeFactors = self.getPrimeFactors(num)
The same for isPrime
:
if self.isPrime(i) and num % i == 0:
Other issues:
range
arguments should be int
not float
, so use int
instead of math.floor
:
range(2, int(math.sqrt(num)))
Use True
not true
in python
return True
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments