Python, smallestFactor()
1.
#Given an integer n (which you can assume is positive),
# return the largest factor of n less than n.
# If n is 1, then you should return 1.
def largestFactor(n):
# TO DO: Finish function
return
The below code is to find the lasrgest factor of any given number.
A number i is factor of a given number n if remainder of n/i = 0
The largest remainder of a number n is the maximum of all remainders excluding n
def largestFactor(n):
if n==1: # when n is 1, the largest factor is 1
return 1
for i in range(1,n): # loop from 1 to (n-1)
if n%i ==0: #if the remainder is 0 then i is a factor of n
res = i # we need the last factor which is the largest factor
return res
print("Enter the number n : ")
n = input()
n = int(n)
res = largestFactor(n)
print("Largest factor of the number ",n,"is: ",res)
Output 1:
Output 2:
Get Answers For Free
Most questions answered within 1 hours.