Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON: This homework requires you to write your code in the form of functions.

ID: 3822942 • Letter: P

Question

PYTHON: This homework requires you to write your code in the form of functions. While the same output can be achieved without using functions. Your code should have functions in them

Write a function, called factor, which takes an integer, say n, as a parameter and returns the first number between 2 and n-1 which divides n. If no such number exists it returns -1

Sample output 1:

Enter n: 15

The smallest divisor is : 3

Sample output 2:

Enter n: 17

The smallest divisor is : -1

Sample output 3:

Enter n: 16

The smallest divisor is : 2

Explanation / Answer

# please copy code from here: https://pastebin.com/kEYKmEkQ as there are indentation issue here. Including code here also for completeness.

def factor(n):
# check for number from 2 to n-1
for x in range(2, n):
# if x divides n return x
# % module operator gives remiander when dividing
if n % x == 0:
return x
  
# if no number divides return -1
return -1

n = int(input("Enter n: "))
print("The smalles divisor is : " + str(factor(n)))