SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from time import time
  2.  
  3.  
  4. def sieve(n):
  5.     """generates primes up to n."""
  6.     s = [True] * (n + 1)
  7.     for p in range(2, n):
  8.         if s[p]:
  9.             yield p
  10.             for i in range(p * p, n, p):
  11.                 s[i] = False
  12.  
  13.  
  14. def is_permutation(n1, n2):
  15.     """returns True if n1 is permutation of n2"""
  16.     to_str_1 = str(n1)
  17.     to_str_2 = str(n2)
  18.     if n1 == n2:
  19.         return False
  20.     to_str_1_digits = {digit: to_str_1.count(digit) for digit in to_str_1}
  21.     to_str_2_digits = {digit: to_str_2.count(digit) for digit in to_str_2}
  22.     if not to_str_1_digits == to_str_2_digits:
  23.         return False
  24.     return True
  25.  
  26.  
  27. def get_permutations(n):
  28.     """generates tuples of 3 permutations each within range n."""
  29.     primes = set(sieve(n))
  30.     for prime1 in primes:
  31.         for prime2 in primes:
  32.             if is_permutation(prime1, prime2):
  33.                 for prime3 in primes:
  34.                     if is_permutation(prime3, prime1) and is_permutation(prime3, prime2):
  35.                         yield prime1, prime2, prime3
  36.  
  37.  
  38. def check_subtraction(n):
  39.     """checks permutations within range n for subtraction rules.
  40.     returns valid permutations."""
  41.     permutations = get_permutations(n)
  42.     for x, y, z in permutations:
  43.         if abs(x - y) == abs(y - z):
  44.             return x, y, z
  45.  
  46.  
  47. if __name__ == '__main__':
  48.     start_time = time()
  49.     x, y, z = sorted(check_subtraction(10000))
  50.     print(str(x) + str(y) + str(z))
  51.     print(f'Time: {time() - start_time} seconds.')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top