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
''' | |
Exercise 4 | |
Create a program that asks the user for a number and then prints out a list of all the divisors of that number. | |
[If you don’t know what a divisor is, it is a number that divides evenly into another number. For example, 13 is | |
a divisor of 26 because 26 / 13 has no remainder.] | |
''' | |
num = int[raw_input["Enter a number: "]] | |
listNum = list[range[1, num+1]] | |
divisor = [] | |
for each in listNum: | |
if num % each == 0: | |
divisor.append[each] | |
print[divisor] |
Four Divisors, is a LeetCode problem from Math subdomain. In this post we will see how we can solve this challenge in Python
Problem Description
You 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
Related
Solved 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:
Input: 1
Output: 6
Explanation: 6 has four divisors 1, 2, 3, and 6.
Difference between any two of them is always greater or equal to 1.Input: 2
Output: 15
Explanation: 15 has four divisors 1, 3, 5 and 15.
Difference between any two of them is always greater or equal to 2Input: 3
Output: 55
Explanation: 55 has four divisors 1, 5, 11 and 55.
Difference between any two of them is always greater than 3.
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++
#include
using
namespace
std;
int
next_prime[
int
x]
{
if
[x == 1 || x == 2] {
return
2;
}
bool
is_prime =
false
;
while
[!is_prime] {
is_prime =
true
;
for
[
int
i = 2; i