Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #H7_christopher.py
  2. # The objective is to practice while loops by finding the greatest factor of a number.
  3.  
  4.  
  5.  
  6. def main():
  7. # Grabs the integer I'll need for later function.
  8. inputV = int(input("Please input an integer: "))
  9. greatest_nontrivial_factor(inputV)
  10.  
  11. def greatest_nontrivial_factor(givenInt):
  12. # if divisor was set to one, it'd return the input as the greatest factor, thus this starts the factoring at two.
  13. divisor = 2
  14. # 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.
  15. while divisor <= givenInt - 1:
  16. # This checks if something IS a factor.
  17. givenInt % divisor
  18. # This updates variable factor every time it finds a factor.
  19. if givenInt % divisor == 0:
  20. factor = divisor
  21. divisor += 1
  22. # Even if the previous IF statement doesn't activate, I still want the loop to increase the divisor.
  23. divisor += 1
  24. print(factor)
  25. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement