Any help in the function exercise (python)

2

The exercise says the following:

"Write a function that receives a positive integer m and returns 1 if m is prime, 0 otherwise."

My attempt:

edit: Then, if the modifications are made, the program is not yet returning values of 1 or 0 (if the number is odd or even).

m = int(input("Digite um numero inteiro:"))    

def recebe_m(m):

  n_divisores = 0
  i = 1

  while(i <= m):        
      if(m%i==0):           
          n_divisores = n_divisores + 1

  i = i+1

  if(n_divisores==2):
      return(1)
 else:
      return(0)

  print(recebe_m(m))

obs: I chose to leave the i

asked by anonymous 21.06.2017 / 06:08

3 answers

2

You need to declare the i variable within the recebe_m function. The same problem will occur with the variable n_divisores , declare the same within the function.

m = int(input("Digite um numero inteiro:"))    
def recebe_m(m):
     n_divisores = 0
     i = 1
     while(i <= m/2):        
        if(m%i==0):           
           n_divisores = n_divisores + 1

        i = i+1

    if(n_divisores==1):
        return(1)
    else:
        return(0)
print(recebe_m(m))

In addition, as the friend of the other response said, you are trying to divide by 0 in the first iteration of your while loop, correct this and initialize i with 1.

The problem with your code is variable scope. In python if you declare a variable in a more internal scope, and the same variable exists in a global scope, the innermost scope variable will be the one used by python to do the calculations.

In your code you try to declare and increment a variable on the same line in a case where the i variable does not exist in the inner scope yet, which causes the error. The same goes for n_divisores .

To better understand:

x = 10
def func():
    x = 5
    print(x)
func() # printa 5
print(x) # printa 10, x com valor 5 morreu.

Edit: As @Anderson said, you do not have to check all the numbers, but only half of them (i <=m/2)

    
21.06.2017 / 13:44
0

For what I could see, did you intend an [int]% 0 in the first case? It does not divide by zero haha (to be q want to open a black hole rsrs)

You need to declare the variable n_divisors within def

the code stays:

m = int(input("Numero: "))
def recebe_m(m):
    cont=0
    for x in range(1,m+1):        
        if(m%x==0):
            cont+=1    
    if(cont==2):
        return(1)
    else:
        return (0)

print(recebe_m(m))
    
21.06.2017 / 10:12
0

I tried to make the function as simple as possible

You do not need to count divisors, if there is any divisor between 1 and half of it, you can already define that the number is not prime.

The following function goes through all the numbers of 2 até m/2 , and if you find some divisor returns 0 , if it ends the loop without finding any, it returns 1 / p>

m = int(input("Digite um numero inteiro:"))

def recebe_m(m):
  for i in range(2, int(m/2)):
    if m%i==0:           
      return(0)

  return(1)

print(recebe_m(m))
    
21.06.2017 / 14:20