Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sympy import factorint, isprime
- def factor(n):
- return " × ".join(f"{p}" if e == 1 else f"{p}^{e}" for p, e in factorint(n).items())
- s = "123456789"
- substrings = [s[i:j] for i in range(len(s)) for j in range(i + 2, len(s) + 1)]
- reversals = sorted([s[::-1] for s in substrings], reverse=True)
- palindromes = [s + (s[:-1])[::-1] for s in substrings]
- palindromes2 = [s + (s[:-1])[::-1] for s in reversals]
- all_results = []
- for strings in (substrings, reversals, palindromes, palindromes2):
- results = []
- for s in strings:
- if len(s) >= 3:
- n = int(s)
- if 0 not in {n % 2, n % 5, n % 3}:
- results.append("\t".join([s, "prime" if isprime(n) else factor(n)]))
- all_results.append("\n".join(results))
- print("\n---\n".join(all_results))
Advertisement
Add Comment
Please, Sign In to add comment