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
def is_prime(self): if self.n == 1: return False for i in range(2, int(self.n/2) + 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, num): while num: self.n, num = num, self.n % num return self.n def get_lcm(self, num): gcd = self.get_gcd(num) lcm = (self.n * num) // gcd return lcm