    # 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

 ''' 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

#### 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

• Discuss
• 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 2

Input: 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 <bits/stdc++.h>`

`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 <= ``sqrt``(x); i++) {`

`            ``if` `(x % i == 0) {`

`                ``is_prime = ``false``;`

`            ``}`

`        ``}`

`        ``x++;`

`    ``}`

`    ``return` `x - 1;`

`}`

`int` `findN(``int` `D)`

`{`

`    ``int` `a = 1 + D;`

`    ``a = next_prime(a);`

`    ``int` `b = a + D;`

`    ``b = next_prime(b);`

`    ``int` `N = a * b;`

`    ``return` `N;`

`}`

`int` `main()`

`{`

`    ``int` `D = 2;`

`    ``int` `ans = findN(D);`

`    ``cout << ans;`

`}`

## Java

`import` `java.io.*;`

`import` `java.lang.*;`

`import` `java.util.*;`

`class` `GFG {`

`  ``static` `int` `next_prime(``int` `x)`

`  ``{`

`    ``if` `(x == ``1` `|| x == ``2``) {`

`      ``return` `2``;`

`    ``}`

`    ``Boolean is_prime = ``false``;`

`    ``while` `(!is_prime) {`

`      ``is_prime = ``true``;`

`      ``for` `(``int` `i = ``2``; i * i <= x; i++) {`

`        ``if` `(x % i == ``0``) {`

`          ``is_prime = ``false``;`

`        ``}`

`      ``}`

`      ``x++;`

`    ``}`

`    ``return` `x - ``1``;`

`  ``}`

`  ``static` `int` `findN(``int` `D)`

`  ``{`

`    ``int` `a = ``1` `+ D;`

`    ``a = next_prime(a);`

`    ``int` `b = a + D;`

`    ``b = next_prime(b);`

`    ``int` `N = a * b;`

`    ``return` `N;`

`  ``}`

`  ``public` `static` `void` `main (String[] args) {`

`    ``int` `D = ``2``;`

`    ``int` `ans = findN(D);`

`    ``System.out.println(ans);`

`  ``}`

`}`

## Python3

`def` `next_prime(x):`

`    ``if` `(x ``=``=` `1` `or` `x ``=``=` `2``):`

`        ``return` `2`

`    ``is_prime ``=` `False`

`    ``while` `(``not` `is_prime):`

`        ``is_prime ``=` `True`

`        ``for` `i ``in` `range``(``2``, ``int``(x ``*``*` `0.5``) ``+` `1``):`

`            ``if` `(x ``%` `i ``=``=` `0``):`

`                ``is_prime ``=` `False`

`        ``x ``+``=` `1`

`    ``return` `x ``-` `1`

`def` `findN(D):`

`    ``a ``=` `1` `+` `D`

`    ``a ``=` `next_prime(a)`

`    ``b ``=` `a ``+` `D`

`    ``b ``=` `next_prime(b)`

`    ``N ``=` `a ``*` `b`

`    ``return` `N`

`D ``=` `2`

`ans ``=` `findN(D)`

`print``(ans)`

## C#

`using` `System;`

`class` `GFG {`

`  ``static` `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 * i <= x; i++) {`

`        ``if` `(x % i == 0) {`

`          ``is_prime = ``false``;`

`        ``}`

`      ``}`

`      ``x++;`

`    ``}`

`    ``return` `x - 1;`

`  ``}`

`  ``static` `int` `findN(``int` `D)`

`  ``{`

`    ``int` `a = 1 + D;`

`    ``a = next_prime(a);`

`    ``int` `b = a + D;`

`    ``b = next_prime(b);`

`    ``int` `N = a * b;`

`    ``return` `N;`

`  ``}`

`  ``public` `static` `void` `Main () {`

`    ``int` `D = 2;`

`    ``int` `ans = findN(D);`

`    ``Console.WriteLine(ans);`

`  ``}`

`}`

## Javascript

`<script>`

`        ``function` `next_prime(x)`

`        ``{`

`            ``if` `(x == 1 || x == 2) {`

`                ``return` `2;`

`            ``}`

`            ``let is_prime = ``false``;`

`            ``while` `(!is_prime) {`

`                ``is_prime = ``true``;`

`                ``for` `(let i = 2; i <= Math.sqrt(x); i++) {`

`                    ``if` `(x % i == 0) {`

`                        ``is_prime = ``false``;`

`                    ``}`

`                ``}`

`                ``x++;`

`            ``}`

`            ``return` `x - 1;`

`        ``}`

`        ``function` `findN(D)`

`        ``{`

`            ``let a = 1 + D;`

`            ``a = next_prime(a);`

`            ``let b = a + D;`

`            ``b = next_prime(b);`

`            ``let N = a * b;`

`            ``return` `N;`

`        ``}`

`        ``let D = 2;`

`        ``let ans = findN(D);`

`        ``document.write(ans);`

`    ``</script>`

Time Complexity:O(N)
Auxiliary Space: O(1)

### 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..

Tải thêm tài liệu liên quan đến bài viết 4 divisors code in python