Advertisement
faubiguy

bracketinfo

Aug 12th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.86 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys, itertools
  3. import urllib.parse
  4. from collections import defaultdict, Counter
  5.  
  6. try:
  7.     import primesieve
  8. except ImportError as e:
  9.     sys.exit("primesieve is required. See https://pypi.python.org/pypi/primesieve")
  10.  
  11. def factorize(n):
  12.     factors = []
  13.     i = 2
  14.     while i * i <= n:
  15.         while n % i == 0:
  16.             n = n // i
  17.             factors.append(i)
  18.         i = i + 1
  19.     factors.append(n)
  20.     return [factor for factor in factors if factor != 1]
  21.    
  22. def get_brackets(n):
  23.     factors = factorize(n)
  24.     if len(factors) == 1:
  25.         return '<' + get_brackets(primesieve.count_primes(n)) + '>'
  26.     else:
  27.         return ''.join(get_brackets(x) for x in factors)
  28.    
  29. def get_ordinal(n, latex=True):
  30.     omega = r'\omega' if latex else 'ω'
  31.     factors = factorize(n)
  32.     if len(factors) == 1:
  33.         if n == 3:
  34.             return omega
  35.         else:
  36.             ordinal = get_ordinal(primesieve.count_primes(n), latex=latex)
  37.             if latex:
  38.                 return omega + '^{' + ordinal + '}'
  39.             else:
  40.                 if '+' in ordinal:
  41.                     return omega + '^(' + ordinal + ')'
  42.                 else:
  43.                     return omega + '^(' + ordinal + ')'
  44.     else:
  45.         terms = []
  46.         for factor, times in sorted(Counter(factors).items(), reverse=True):
  47.             if factor == 2:
  48.                 terms.append(str(times))
  49.             else:
  50.                 terms.append(get_ordinal(factor, latex=latex) + (str(times) if times > 1 else ''))
  51.         return '+'.join(terms)
  52.    
  53. def full_ordinal(n):
  54.     return r'\mathfrak{p}(' + get_ordinal(n) + ')'
  55.  
  56. def is_symmetrical(n):
  57.     factors = factorize(n)
  58.     if len(factors) == 1:
  59.         return is_symmetrical(primesieve.count_primes(n))
  60.     found_odd = False
  61.     for factor, times in Counter(factors).items():
  62.         if times % 2 == 1:
  63.             if found_odd:
  64.                 return False
  65.             if not is_symmetrical(factor):
  66.                 return False
  67.             found_odd = True
  68.     return True
  69.            
  70.  
  71. def get_info(n):
  72.     info = {}
  73.     brackets = get_brackets(n)
  74.     factors = factorize(n)
  75.    
  76.     info["bracket_notation"] = brackets
  77.    
  78.     abc = brackets.replace('<>', 'a')
  79.     for letter in 'abcdefghij':
  80.         abc = abc.replace('<'+letter+'>', chr(ord(letter)+1))
  81.     info["abc_notation"] = abc
  82.    
  83.     p = abc.replace('<', 'p<')
  84.     for letter in 'abcdefghij':
  85.         p = p.replace(letter, 'p'*(ord(letter)-96)+'1')
  86.     info["p_notation"] = p
  87.  
  88.     digits_list = []
  89.     last_bracket = ''
  90.     for char in brackets:
  91.         if char == last_bracket:
  92.             digits_list[-1] += 1
  93.         else:
  94.             digits_list.append(1)
  95.         last_bracket = char
  96.     digits = ''.join(map(str,digits_list))
  97.     info["digit_notation"] = digits
  98.    
  99.     depth = []
  100.     current_depth = 0
  101.     max_depth = 0
  102.     add = True
  103.     for digit in digits_list[:-1]:
  104.         if add:
  105.             current_depth += digit
  106.         else:
  107.             current_depth -= digit
  108.         depth.append('*' * current_depth)
  109.         add = not add
  110.         if current_depth > max_depth:
  111.             max_depth = current_depth
  112.     info["depth_notation"] = '\n'.join(''.join(row) for row in itertools.zip_longest(*depth, fillvalue=' '))
  113.     ordinal_escaped = urllib.parse.quote(full_ordinal(n))
  114.     info["ordinal"] = '[img]http://latex.codecogs.com/gif.latex?' + ordinal_escaped + '[/img]'
  115.    
  116.     info["is_prime"] = len(factors) == 1
  117.     info["is_symmetrical"] = is_symmetrical(n)
  118.     info["is_alphabetical"] = not ('<' in abc)
  119.  
  120.     info["length"] = len(brackets)
  121.     info["reversals"]= len(digits_list) - 1
  122.     info["max_depth"] = max_depth
  123.     info["factors"] = len(factors)
  124.     info["smoothness"] = max(factors)
  125.     info["necessary_brackets"] = abc.count('<')*2
  126.    
  127.     return info
  128.  
  129. def info_string(number):
  130.     info = get_info(number)
  131.     return '\n'.join([
  132.         str(number),
  133.         info["bracket_notation"],
  134.         info["abc_notation"],
  135.         info["p_notation"],
  136.         info["digit_notation"],
  137.         '[code]',
  138.         info["depth_notation"],
  139.         '[/code]' +
  140.         info["ordinal"],
  141.         '',
  142.         'Prime: {0}'.format('Yes' if info["is_prime"] else 'No'),
  143.         'Symmetrical: {0}'.format('Yes' if info["is_symmetrical"] else 'No'),
  144.         'Alphabetical: {0}'.format('Yes' if info["is_alphabetical"] else 'No'),
  145.         '',
  146.         'Length: {0}'.format(info["length"]),
  147.         'Reversals: {0}'.format(info["reversals"]),
  148.         'Max Depth: {0}'.format(info["max_depth"]),
  149.         'Factors: {0}'.format(info["factors"]),
  150.         'Smoothness: {0}'.format(info["smoothness"]),
  151.         'Necessary Brackets: {0}'.format(info["necessary_brackets"])
  152.     ])
  153.  
  154. if __name__ == '__main__':
  155.     n = int(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read())
  156.     print(info_string(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement