N
N
numb72016-02-16 17:49:36
Python
numb7, 2016-02-16 17:49:36

How to find the minimum natural divisor of a number x?

Good evening!
Can you please tell me how to find the minimum natural divisor of a number x other than 1, i.e. y> 1?
For example, if the input is the number x=10, then y=2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2016-02-16
@denshi

Isn't this a very simple question? Obviously, you just need to divide cyclically by numbers, starting with 2, and check whether they divide the original number.

for i in range(2, x+1):
    if not x % i:
        print(i)
        break

If the number is large, then it is better to stop the loop at the root of x:
import math
for i in range(2, int(math.sqrt(x))+1):
    if not x % i:
        print(i)
        break
else:
    print(x)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question