gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
Create a program that has a Class named Number, which is initialized with a positive…
- User
Create a program that has a Class named Number, which is initialized with a positive integer 'n' as its sole argument. It should have at least four methods:
is_prime returns True if 'n' is prime, and False if not. 1 is not prime. get_divisors returns a list of positive divisors of 'n', in ascending order. get_gcd takes a positive integer argument, and returns the greatest common divisor of 'n' and the argument. get_lcm takes a positive integer argument, and returns the least common (positive) multiple of 'n' and the argument.
- Assistant
class Number: def init(self, n): self.n = n
codedef is_prime(self): if self.n <= 1: return False for i in range(2, int(self.n**(0.5))+1): if self.n % i == 0: return False return True def get_divisors(self): divisors = [] for i in range(1, self.n+1): if self.n % i == 0: divisors.append(i) return divisors def get_gcd(self, m): while m != 0: self.n, m = m, self.n % m return self.n def get_lcm(self, m): lcm = (self.n * m) // self.get_gcd(m) return lcm