Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #H7_christopher.py
- # The objective is to practice while loops by finding the greatest factor of a number.
- def main():
- # Grabs the integer I'll need for later function.
- inputV = int(input("Please input an integer: "))
- greatest_nontrivial_factor(inputV)
- def greatest_nontrivial_factor(givenInt):
- # if divisor was set to one, it'd return the input as the greatest factor, thus this starts the factoring at two.
- divisor = 2
- # In order to factor, I'm using a while loop to count from 2 all the way up to the number minus 1 to check all the divisors, then I want to take the greatest number of that returned list.
- while divisor <= givenInt - 1:
- # This checks if something IS a factor.
- givenInt % divisor
- # This updates variable factor every time it finds a factor.
- if givenInt % divisor == 0:
- factor = divisor
- divisor += 1
- # Even if the previous IF statement doesn't activate, I still want the loop to increase the divisor.
- divisor += 1
- print(factor)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement