Guest User

substring-primes.py

a guest
Aug 4th, 2025
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | Source Code | 0 0
  1. from sympy import factorint, isprime
  2.  
  3. def factor(n):
  4.     return " × ".join(f"{p}" if e == 1 else f"{p}^{e}" for p, e in factorint(n).items())
  5.  
  6. s = "123456789"
  7.  
  8. substrings = [s[i:j] for i in range(len(s)) for j in range(i + 2, len(s) + 1)]
  9. reversals = sorted([s[::-1] for s in substrings], reverse=True)
  10. palindromes = [s + (s[:-1])[::-1] for s in substrings]
  11. palindromes2 = [s + (s[:-1])[::-1] for s in reversals]
  12.  
  13. all_results = []
  14.  
  15. for strings in (substrings, reversals, palindromes, palindromes2):
  16.     results = []
  17.     for s in strings:
  18.         if len(s) >= 3:
  19.             n = int(s)
  20.             if 0 not in {n % 2, n % 5, n % 3}:
  21.                 results.append("\t".join([s, "prime" if isprime(n) else factor(n)]))
  22.     all_results.append("\n".join(results))
  23.  
  24. print("\n---\n".join(all_results))
  25.  
Advertisement
Add Comment
Please, Sign In to add comment