Advertisement
banovski

Maximum prime divisor (functional)

Dec 3rd, 2021 (edited)
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # The script calculates maximum prime divisor of a positive integer.
  4. # Apart from input and output instructions the code's purely
  5. # functional (as far as I can judge). The script may exceed the
  6. # maximum recursion depth or make the interpreter freeze.
  7.  
  8. def divisorGenerator(a, b):
  9.     return divisibilityCheck(a, a // (a // b + 1))
  10.  
  11. def divisibilityCheck(a, b):
  12.     if a % b == 0:
  13.         return primeCheck(a, b)
  14.     else:
  15.         return limitCheck(a, b)
  16.  
  17. def limitCheck(a, b):
  18.     if b > 1:
  19.         return divisorGenerator(a, b)
  20.     else:
  21.         return b
  22.  
  23. def primeCheck(a, b):
  24.     if not [c for c in range(2, b // 2 + 1) if b % c == 0]:
  25.         return b
  26.     else:
  27.         return limitCheck(a, b)
  28.  
  29. def intializer(a):
  30.     return limitCheck(a, a // 2)
  31.  
  32. a = None
  33. while not a:
  34.     a = input("Enter a positive decimal integer to start evaluation or a non-number to exit: ")
  35.     if a.isnumeric():
  36.         try:
  37.             print(intializer(int(a)))
  38.         except:
  39.             print("Ouch! This one didn't go down well!")
  40.  
  41.         a = None
  42.     else:
  43.         exit("Oh, well...")
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement