Prime numbers with Python

Asked

Viewed 276 times

-1

Trying a thousand ways to get this result.
Always returns the same thing.

number = int(input('Numero: '))

if number >= 1:
    for i in range(1, number):
        if number % i != 0:
            print(number, 'é primo')
        else:
            print(number, 'não é primo')
            break
elif number == 0:
    print(number, 'é zero')
else:
    print(number, 'é negativo')

Ex1 :
Number: 6
6 is not cousin

Ex2 :
Number: 11
11 is not cousin

4 answers

1

Complementing the other answers, it is also worth commenting that half of the iterations of

for i in range(1, number):

are useless. Note that no number is divisible by one greater than its half. To understand better we will observe some things:

Any number divided by its half results in 2.

That’s a fact: 10/5 = 2, 20/10 = 2, 14/7 = 2 etc. This implies that any number divided by another that is greater than its half results in a value less than 2. 10/6 = 1.6, 20/11 = 1.81 etc.

The only way a division can result in 1 is dividing a number by himself

With these two absolute truths, we can move on to the mathematical language that, for any number x:

>>> x/x = 1 #Um número dividido por ele mesmo resulta em 1

>>> x/(x/2) = 2 #Um número dividido pela sua metade resulta 2

and affirm with certainty that x divided by any number y such that x/2 < y < x results in some value z such that 1 < z < 2. And none of them will be whole. Ie, We know by definition for these values, number % i != 0 will always return True, it is therefore a waste of resources to test them. it is interesting, then, that your for be changed to:

for i in range(1, int(number/2)+1):

What should work for both even and odd numbers. Incidentally, it is worth remembering that no even number is prime (except for the 2)

  • 2

    Better than that, you can do the division tests up to the x, do not need to go up to x/2.

  • Yeah, there’s a very detailed explanation for that. If I’m not mistaken about hkotsubo, in some post here on the site, but I can not find to link here.

  • 1

    @Yoyo Tem that one (and that one also, except that it is not in Python - but the idea is the same) - even has other improvements suggested (such as not testing pairs, etc)

  • 1

    @hkotsubo I was referring to that one Which although not specifically about prime numbers, has everything to do. Worth reading

  • Even though the input is already an integer?

  • If you mean the int(number/2). Yes, because the result of dividing an odd number by 2 will result in a float, but we need an integer number to use within the function range(). the int(number/2) will take the entire part of the divisions that result in float

Show 1 more comment

1

I found the answer!!
And I added a few extras.

# numeros primos
if number > 1:
    for i in range(2, number):
        if number % i == 0:
            print(number, 'não é primo')
            break
    else:
        print(number, 'é primo')
elif number == 0:
    print(number, 'é zero')
elif number == 1:
    print(number, 'é um')
else:
    print(number, 'é negativo')
  • Exactly. I think if you replace ">=" with ">" it already solves. Thanks!!

0

A line (a list) for primes smaller than 50...

[len(x) for x in list(map(lambda x: [operator.mod(len(range(1,x)), z) for z in range(1,x)], [item for item in range(1,50)])) if x.count(0) == 2]

0

A number is prime when it is divisible by 1 and by itself.

The variable ePrimo shall count the number of number, if this amount is exactly equal to 2 then number % i == 0 was only true in two conditions, when i equal to or equal to 1 i equal to number. Then only in this situation will the number be prime.

number = int(input('Numero: '))
ePrimo = 0

for i in range(1, (number + 1)):        
  if number % i == 0:
    ePrimo += 1
    
if ePrimo  == 2 :
  print('primo')
else:
  print('nao primo')
  • Well that. Had passed hit. Valeu!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.