python 3, exercise with prime numbers

Asked

Viewed 427 times

2

The smallest m such that n · m + 1 don’t be a cousin.

Example: For the first example, 3 · 1 + 1 = 4, the exit will be 1.

For the second example, 4 · 1 + 1 = 5, we can’t print 1 because 5 is cousin.

However, m = 2 is all right, since 4 · 2 + 1 = 9, that is not cousin. For the third example, 10 · 2 + 1 = 21, print 2.

I wrote the following code but this giving error:

n=int(input())
z=2 
m=1
div=0
x =(n*m+1)
if n>=1 and n<=1000:
           while z<=x-1:
               if x%z==0:
                   div=div+1
               z=z+1
           if div==0:
                m=m+1
                x=n*m+1
           if x==1 or x==2 or x==3:
               m=m+1
               x=n*m+1
           print(m)

2 answers

0

Unfortunately, I haven’t come up with a solution yet, but I rewrote parts of your code in a way that made it easier to understand, without affecting the results.

n = int(input('n: '))

z = 2

m = 1

div = 0

x = (n * m + 1)


if n in range(1,1001): # n >= 1 and n <= 1000

           while z <= x - 1:

               if x % z == 0:
                   div = div + 1
               z = z + 1

           if div == 0:
                m = m + 1
                x = n * m + 1

           if  x >= 1 and x <= 3: # x == 1 or x == 2 or x == 3:  
               m = m + 1
               x = n * m + 1

           print(m)

Follow a method that checks if a number is prime that can help you in the solution.

# Recebe um numero inteiro e retorna ele mesmo caso seja um número primo

def verificar_primo(numero):    
    total = 0
    for n in range(1, numero + 1):        
        if numero % n == 0:
            total += 1        
    if total == 2:
        # É primo porque o resto da divisão foi igual a zero duas vezes
        # Em outras palavras, o número foi divisivel por 1 e por ele mesmo
        return numero
  • I stalled in trying to help you in math because, for example, I didn’t understand the role of the 'z' variable and why it was initialized with the value 2. It would be nice if you included some comments in your code explaining what you are doing.

  • he used the variable z just to symbolize the 2 that in the case is the condition of validation of prime numbers, which I found a little unnecessary to create a variable for a constant.

  • Ah, I get it, I’ll try to figure out... hug!

  • As you can see I already solved, but it’s okay if you want to too! :)

0


I believe this is a valid solution to your problem:

import math


def fast_prime(numero):
    """Função de retorno rapido de números primos"""
    if numero == 1:
        return False

    if numero == 2:
        return True

    if numero == 9:   # Por alguma razão o 9 entra como primo nesse código 
        return False  #  então tive que valida-lo

    if numero % 2 == 0:
        return False

    maiorraiz = int(math.ceil(math.sqrt(numero)))  
    for i in range(3, maiorraiz, 2):
        if numero % i == 0:
            return False

    return True


numero = int(input('Digite um número: '))

multiplicador = 1

equacao = numero * multiplicador + 1

while True:
    if fast_prime(equacao):
        multiplicador += 1
        equacao = numero * multiplicador +1
    else:
        print(f'O valor que satisfaz a equação é {multiplicador}')
        break

I hope I helped. Any questions, ask :)

  • No need to convert return from math.ceil() for int(), for ceil() returns an integer already. E 9 returns True because the code try to traverse a range(3, 3), then he doesn’t even get into the loop and return True straightforward.

  • Got it, thanks for the tip!

Browser other questions tagged

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