- #1
zeion
- 466
- 1
Hi,
This compiles, I just wanted suggestions for fewer lines. Thanks.
Need to write a function for this:
Given an int variable, return a factor of that int. If the input is not between 0 and 100 return -1. If there are more than one distinct factors, return the second smallest. 1 does not count as a factor.
def find_factor(x):
if x > 1 and x < 100: 'Checks if x is in the range'
for a in range(2, x+1): "1 doesn't count, so skip it and we need to get up to x itself for the primes"
if x%a==0: 'Check if it is a factor'
return a
elif x == 1: 'Special case for 1'
return 1
else:
return -1 'For out of range'
This compiles, I just wanted suggestions for fewer lines. Thanks.
Homework Statement
Need to write a function for this:
Given an int variable, return a factor of that int. If the input is not between 0 and 100 return -1. If there are more than one distinct factors, return the second smallest. 1 does not count as a factor.
Homework Equations
The Attempt at a Solution
def find_factor(x):
if x > 1 and x < 100: 'Checks if x is in the range'
for a in range(2, x+1): "1 doesn't count, so skip it and we need to get up to x itself for the primes"
if x%a==0: 'Check if it is a factor'
return a
elif x == 1: 'Special case for 1'
return 1
else:
return -1 'For out of range'