4 divisors code in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Four Divisors, is a LeetCode problem from Math subdomain. In this post we will see how we can solve this challenge in Python Problem DescriptionYou can find the full details of the problem Four Divisors at LeetCode Solution: Please check the main.py snippet for the solution. This solution originally posted at: Github by @kamyu104 RelatedSolved something today?Dump your day to day learning, note and quick solution. Let other programmers / developers / software engineers learn from you Vote this View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given the number D, find the smallest number N such that it has exactly four divisors and the difference between any two of them is greater than or equal to D. Examples:
Approach: It is obvious that ‘1’ and the number itself are the divisors of N. So the number which has exactly 4 divisors has its divisors as 1, a, b, a*b respectively. For the condition that it has exactly 4 divisors, both a and b must be prime. For the condition that the difference between any two of them should at least be D, start finding a from 1+d and check whether it is prime or not, If it is not prime then we will find the prime number just next to it. Similarly, start finding b from a + d and check whether it is prime or not, and do the same as done for finding a. Below is the implementation of the above approach. C++
Java
Python3
C#
Javascript
Time Complexity:O(N) How do you find divisors in Python?Finding divisors of a number with Python. def get_divisors(n): for i in range(1, int(n / 2) + 1): if n % i == 0: yield i yield n. ... . def prime_factors(n): i = 2 while i * i <= n: if n % i == 0: n /= i yield i else: i += 1 if n > 1: yield n.. |