Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys, itertools
- import urllib.parse
- from collections import defaultdict, Counter
- try:
- import primesieve
- except ImportError as e:
- sys.exit("primesieve is required. See https://pypi.python.org/pypi/primesieve")
- def factorize(n):
- factors = []
- i = 2
- while i * i <= n:
- while n % i == 0:
- n = n // i
- factors.append(i)
- i = i + 1
- factors.append(n)
- return [factor for factor in factors if factor != 1]
- def get_brackets(n):
- factors = factorize(n)
- if len(factors) == 1:
- return '<' + get_brackets(primesieve.count_primes(n)) + '>'
- else:
- return ''.join(get_brackets(x) for x in factors)
- def get_ordinal(n, latex=True):
- omega = r'\omega' if latex else 'ω'
- factors = factorize(n)
- if len(factors) == 1:
- if n == 3:
- return omega
- else:
- ordinal = get_ordinal(primesieve.count_primes(n), latex=latex)
- if latex:
- return omega + '^{' + ordinal + '}'
- else:
- if '+' in ordinal:
- return omega + '^(' + ordinal + ')'
- else:
- return omega + '^(' + ordinal + ')'
- else:
- terms = []
- for factor, times in sorted(Counter(factors).items(), reverse=True):
- if factor == 2:
- terms.append(str(times))
- else:
- terms.append(get_ordinal(factor, latex=latex) + (str(times) if times > 1 else ''))
- return '+'.join(terms)
- def full_ordinal(n):
- return r'\mathfrak{p}(' + get_ordinal(n) + ')'
- def is_symmetrical(n):
- factors = factorize(n)
- if len(factors) == 1:
- return is_symmetrical(primesieve.count_primes(n))
- found_odd = False
- for factor, times in Counter(factors).items():
- if times % 2 == 1:
- if found_odd:
- return False
- if not is_symmetrical(factor):
- return False
- found_odd = True
- return True
- def get_info(n):
- info = {}
- brackets = get_brackets(n)
- factors = factorize(n)
- info["bracket_notation"] = brackets
- abc = brackets.replace('<>', 'a')
- for letter in 'abcdefghij':
- abc = abc.replace('<'+letter+'>', chr(ord(letter)+1))
- info["abc_notation"] = abc
- p = abc.replace('<', 'p<')
- for letter in 'abcdefghij':
- p = p.replace(letter, 'p'*(ord(letter)-96)+'1')
- info["p_notation"] = p
- digits_list = []
- last_bracket = ''
- for char in brackets:
- if char == last_bracket:
- digits_list[-1] += 1
- else:
- digits_list.append(1)
- last_bracket = char
- digits = ''.join(map(str,digits_list))
- info["digit_notation"] = digits
- depth = []
- current_depth = 0
- max_depth = 0
- add = True
- for digit in digits_list[:-1]:
- if add:
- current_depth += digit
- else:
- current_depth -= digit
- depth.append('*' * current_depth)
- add = not add
- if current_depth > max_depth:
- max_depth = current_depth
- info["depth_notation"] = '\n'.join(''.join(row) for row in itertools.zip_longest(*depth, fillvalue=' '))
- ordinal_escaped = urllib.parse.quote(full_ordinal(n))
- info["ordinal"] = '[img]http://latex.codecogs.com/gif.latex?' + ordinal_escaped + '[/img]'
- info["is_prime"] = len(factors) == 1
- info["is_symmetrical"] = is_symmetrical(n)
- info["is_alphabetical"] = not ('<' in abc)
- info["length"] = len(brackets)
- info["reversals"]= len(digits_list) - 1
- info["max_depth"] = max_depth
- info["factors"] = len(factors)
- info["smoothness"] = max(factors)
- info["necessary_brackets"] = abc.count('<')*2
- return info
- def info_string(number):
- info = get_info(number)
- return '\n'.join([
- str(number),
- info["bracket_notation"],
- info["abc_notation"],
- info["p_notation"],
- info["digit_notation"],
- '[code]',
- info["depth_notation"],
- '[/code]' +
- info["ordinal"],
- '',
- 'Prime: {0}'.format('Yes' if info["is_prime"] else 'No'),
- 'Symmetrical: {0}'.format('Yes' if info["is_symmetrical"] else 'No'),
- 'Alphabetical: {0}'.format('Yes' if info["is_alphabetical"] else 'No'),
- '',
- 'Length: {0}'.format(info["length"]),
- 'Reversals: {0}'.format(info["reversals"]),
- 'Max Depth: {0}'.format(info["max_depth"]),
- 'Factors: {0}'.format(info["factors"]),
- 'Smoothness: {0}'.format(info["smoothness"]),
- 'Necessary Brackets: {0}'.format(info["necessary_brackets"])
- ])
- if __name__ == '__main__':
- n = int(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read())
- print(info_string(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement