Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. from math import sqrt
  2. from bisect import bisect_left
  3.  
  4. def binarySearch(arr, l, r, x):
  5.     while l <= r:
  6.         mid = int(l + (r - l)/2);
  7.         if arr[mid] == x:
  8.             return mid
  9.         elif arr[mid] < x:
  10.             l = mid + 1
  11.         else:
  12.             r = mid - 1
  13.     return -1
  14.  
  15. def sieve_for_primes_to():
  16.     n = 1000000
  17.     size = n//2
  18.     sieve = [1]*size
  19.     limit = int(n**0.5)
  20.     for i in range(1,limit):
  21.         if sieve[i]:
  22.             val = 2*i+1
  23.             tmp = ((size-1) - i)//val
  24.             sieve[i+val::val] = [0]*tmp
  25.     return [2] + [i*2+1 for i, v in enumerate(sieve) if v and i>0]
  26.  
  27. primes = sieve_for_primes_to()
  28.  
  29. def find_ge(a, key):
  30.     i = bisect_left(a, key)
  31.     return a[i+1]
  32.  
  33. def find_next_prime(p):
  34.     return find_ge(primes, p)
  35.  
  36. def factorize(n):
  37.     distinct_factors = []
  38.     if binarySearch(primes, 0, len(primes)-1, n) == True:
  39.         distinct_factors.append(n)
  40.     else:
  41.         for x in range(2, int(sqrt(n))+1):
  42.             if n % x == 0:
  43.                 distinct_factors.append(x)
  44.                 if x * x != n:
  45.                     distinct_factors.append(int(n/x))
  46.     return distinct_factors
  47.  
  48. x = 2
  49. total = 2
  50. used = set({2})
  51. last_output = 0
  52. count_outputs = 1
  53.  
  54. for t in range(100000):
  55.     x = find_next_prime(x)
  56.     flag = False
  57.     total = sum(used)
  58.     if total != x:
  59.         while total % x == 0:  
  60.             remains = factorize(int(total / x))
  61.             for r in remains:
  62.                 if r in used:
  63.                     flag = True
  64.                     break
  65.             if flag == True:
  66.                 break
  67.             print("Found cyclic", x)
  68.             x = find_next_prime(x)
  69.  
  70.     total += x
  71.    
  72.     used.add(x)
  73.     distinct_factors = factorize(total)
  74.  
  75.     for f in distinct_factors:
  76.         if f in used:
  77.             used.remove(f)
  78.         min_used = min(used)
  79.         if last_output != min_used:
  80.             if (last_output != 0):
  81.                 print("{}:".format(last_output), count_outputs)
  82.             count_outputs = 1
  83.             last_output = min_used
  84.         else:
  85.           count_outputs += 1
  86.  
  87.     x = max(used)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement