J
J
Janna16032020-05-10 14:23:53
Python
Janna1603, 2020-05-10 14:23:53

How to write a minimum divisor problem?

Given an integer not less than 2. Print its least natural divisor different from 1.
When you first enter a number less than 2, and then more than two, it gives an error
. And when you immediately enter a number greater than two, everything is fine
b = 1
while True:
a = int (input())
b = b + 1
if a % b == 0:
print(b)
break
else:
if a < 2:
print("Error!")
continue

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ronald McDonald, 2020-05-10
@Zoominger

else:
if a < 2:
print("Ошибка!")
continue

Right here.
You need to check the value of the number before division. If less, then break .

D
Drill, 2020-05-10
@Drill

def small_div(num):
    if num < 2:
        return f'Число не соответствует условию задачи: {num} < 2'
    for i in range(2, num+1):
        if not num%i:
            return i

A
Andrew, 2020-05-10
@OdAs

There is a special Euclid algorithm for the largest divisor:
a = int(input())
b = int(input())
while a != 0 and b != 0:
if a > b:
a %= b
else :
b %= a
gcd = a+b
print(gcd)
you just need to change it a bit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question