Computer Science, asked by rizwanatabasuum1024, 1 year ago


def f(n):
s=0
for i in range(1,n+1):
if n%i == 0:
s = s+1
return(s%2 == 1)

The function f(n) given above returns True for a positive number n if and only if:
n is a ?

Answers

Answered by bhagyashreechowdhury
4

Answer:

The function f(n) returns True for a positive number n if and only if, n is a square of a number i.e., n has even number of factors.

Explanation:

Since indentation is very important in case of python programming therefore we consider the code given in the question to be written as follows:

def f(n):

   s=0

   for i in range(1,n+1):

       if n%i == 0:

           s= s+1

   return(s%2 == 1)

The above function returns True for a positive number n if and only if, n is a square of a number i.e., n has even number of factors .

Let us consider two scenarios, for explaining the program:

Scenario 1: Let the value of n be 4

f(4):

    s = 0  

Since, n=4, the range of the 'for' loop will be till 5 (n+1)  

for i in range(1,5):  

Executing the loop for the 1st time:  

     4%1 == 0

     Therefore, incrementing the value of s by 1, which means now s=0+1=1  

Executing the loop for the 2nd time:  

     4%2 == 0

    Therefore, incrementing the value of s by 1, which means now s=1+1=2  

Executing the loop for the 3rd time:  

     4%3 != 0

    Therefore, not incrementing the value of s by 1, which means stays as s=2

Executing the loop for the 4th time:  

     4%4 == 0

    Therefore, incrementing the value of s by 1, which means now s=2+1=3

Coming out of the loop, as the maximum iteration has been reached:  

return(3%2 ==1) # Since the value of s is 3 at the end of the loop

The above statement is true and hence the function returns True.

Scenario 2: Let the value of n be 5

f(5):

    s = 0  

Since, n=5, the range of the 'for' loop will be till 6 (n+1)  

for i in range(1,6):

Executing the loop for the 1st time:  

     5%1 == 0

     Therefore, incrementing the value of s by 1, which means now s=0+1=1

Executing the loop for the 2nd time:  

     5%2 != 0

    Therefore, not incrementing the value of s, which means s stays as s=1

Executing the loop for the 3rd time:  

     5%3 != 0

    Therefore, not incrementing the value of s by 1, which means s stays as s=1

Executing the loop for the 4th time:  

     5%4 == 0

    Therefore, not incrementing the value of s by 1, which means s stays as s=1

Executing the loop for the 5th time:  

     5%5 == 0

    Therefore, incrementing the value of s by 1, which means s is now s=1+1=2

Coming out of the loop, as the maximum iteration has been reached:  

return(2%2 ==1) # Since the value of s is 2 at the end of the loop

The above statement is false and hence the function returns False.

Answered by lovingheart
2

Answer:

True for a positive number n if and only if where n is a perfect square.

Explanation:

When n = 1, the value of s will be 1 and returns false

When n = 2, the value of s will be 2 and returns true

When n = 3, the value of s will be 2 and returns false

When n = 4, the value of s will be 3 and returns true

When n = 5, the value of s will be 4 and returns false

When n = 6, the value of s will be 4 and returns false

When n = 7, the value of s will be 2 and returns false

….

When n = 9, the value of s will be 3 and returns true

…..

When n=16 the value of s will be 3 and returns true

From the above values we can state that whenever the value is a perfect square, the function returns true.

Similar questions