Advertisement
Radeen10-_

Permutated Primes

Dec 17th, 2021 (edited)
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.56 KB | None | 0 0
  1. @print the prime numbers that have permutated primes and Remove Duplicate primes Such as 13:31 and 31:13.
  2. Empty list means no prime permutation is available
  3.        
  4. An example is given below where the numbers are under 300:
  5.    
  6.  =====================================================================================================================================  
  7. {2: [], 3: [], 5: [], 7: [], 11: [], 13: [31], 17: [71], 19: [], 23: [], 29: [], 37: [73], 41: [], 43: [], 47: [], 53: [], 59: [], 61: [], 67: [], 79: [97], 83: [], 89: [], 101: [], 107: [701], 109: [], 113: [131, 311], 127: [271], 137: [317, 173], 139: [193], 149: [419, 941, 491], 151: [], 157: [571, 751], 163: [613, 631], 167: [617, 761], 179: [971, 197, 719], 181: [811], 191: [911], 199: [919, 991], 211: [], 223: [], 227: [], 229: [], 233: [], 239: [293], 241: [421], 251: [521], 257: [], 263: [], 269: [], 277: [727], 281: [821], 283: [823]}
  8.  
  9.  
  10.  
  11.  
  12.  
  13. TASK
  14. =====================================================================================================================================
  15. The prime 149 has 3 permutations which are also primes: 419, 491 and 941.
  16.  
  17. There are 3 primes below 1000 with three prime permutations:
  18. 149 ==> 419 ==> 491 ==> 941
  19. 179 ==> 197 ==> 719 ==> 971
  20. 379 ==> 397 ==> 739 ==> 937
  21. But there are 9 primes below 1000 with two prime permutations:
  22.  
  23. 113 ==> 131 ==> 311
  24. 137 ==> 173 ==> 317
  25. 157 ==> 571 ==> 751
  26. 163 ==> 613 ==> 631
  27. 167 ==> 617 ==> 761
  28. 199 ==> 919 ==> 991
  29. 337 ==> 373 ==> 733
  30. 359 ==> 593 ==> 953
  31. 389 ==> 839 ==> 983
  32. Finally, we can find 34 primes below 1000 with only one prime permutation:
  33.  
  34. [13, 17, 37, 79, 107, 127, 139, 181, 191, 239, 241, 251, 277, 281, 283, 313, 347, 349, 367, 457, 461, 463, 467, 479, 563, 569, 577, 587, 619, 683, 709, 769, 787, 797]
  35. Each set of permuted primes are represented by its smallest value, for example the set 149, 419, 491, 941 is represented by 149, and the set has 3 permutations.
  36.  
  37. Notes
  38.  
  39. the original number (149 in the above example) is not counted as a permutation;
  40. permutations with leading zeros are not valid
  41. Your Task
  42. Your task is to create a function that takes two arguments:
  43.  
  44. an upper limit (n_max) and
  45. the number of prime permutations (k_perms) that the primes should generate below n_max
  46. The function should return the following three values as a list:
  47.  
  48. the number of permutational primes below the given limit,
  49. the smallest prime such prime,
  50. and the largest such prime
  51. If no eligible primes were found below the limit, the output should be [0, 0, 0]
  52.  
  53. Examples
  54. Let's see how it would be with the previous cases:
  55.  
  56. permutational_primes(1000, 3) ==> [3, 149, 379]
  57. ''' 3 primes with 3 permutations below 1000, smallest: 149, largest: 379 '''
  58.  
  59. permutational_primes(1000, 2) ==> [9, 113, 389]
  60. ''' 9 primes with 2 permutations below 1000, smallest: 113, largest: 389 '''
  61.  
  62. permutational_primes(1000, 1) ==> [34, 13, 797]
  63. ''' 34 primes with 1 permutation below 1000, smallest: 13, largest: 797 '''
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. @code
  74.  
  75.  
  76.  
  77. import math
  78. import itertools
  79.  
  80. def prime(n):
  81.    primes=[]
  82.    for i in range(2,n+1):
  83.        primes.append(i)
  84.    i=2
  85.    while (i<=(math.sqrt(n))):
  86.        if i in primes:
  87.            for j in range(i*2,n+1,i):
  88.                if j in primes:
  89.                    primes.remove(j)
  90.        i+=1
  91.    a = [int(k) for k in primes]
  92.    import collections
  93.    numbers = collections.OrderedDict()
  94.    for ele in a:
  95.        x = ''.join(sorted(str(ele)))
  96.        if x not in numbers:
  97.            numbers[x] = []
  98.        numbers[x].append(int(ele))
  99.    a1= [v[0] for v in numbers.values()]
  100.    return a1
  101.  
  102. #     return
  103. def permutational_prime(n_max,k_perms):
  104.    a1 = prime(n_max)
  105.    lst1=[]
  106.    for j in a1:
  107.        j = str(j)
  108.        perm_iterator = itertools.permutations(j)
  109.        lst = []
  110.        for item in perm_iterator:
  111.                x = int(''.join(item))
  112.                if x> 1 and x<n_max:
  113.                    for j in range(2, int(math.sqrt(x)) + 1):
  114.                        if (x% j) == 0:
  115.                            break
  116.                    else:
  117.                        lst.append(x)
  118.  
  119.        lst1.append(lst)
  120.    dup_lst_sort={i[0]:i[1:] for i in lst1}
  121.    dict = {key: list(set(value)) for key, value in dup_lst_sort.items()}
  122.  
  123.    l = []  # list for keys
  124.    for key in dict:  # for each key
  125.        if len(dict[key]) > 0:
  126.            for item in dict[key]:
  127.                if len(str(key)) != len(str(item)):  # if the length of key is different from length of element in the list
  128.                    # try:
  129.                    #     l.index(key)  # see if they key is already there
  130.                    # except:  # if not
  131.                    #     l.append(key)  # add key to list
  132.                    dict[key].remove(item)
  133.  
  134.    for key in dict:  # for each key
  135.        if len(dict[key]) > 0:
  136.            for item in dict[key]:
  137.                if len(str(key)) != len(str(item)):  # if the length of key is different from length of element in the list
  138.                    try:
  139.                        l.index(key)  # see if they key is already there
  140.                    except:  # if not
  141.                        l.append(key)  # add key to list
  142.    for k in l:  # for each key in the list
  143.        del dict[k]  # delete key
  144.    for k,v in dict.items():
  145.        if k in v:
  146.            v.remove(k)
  147.    final=[]
  148.    for key,value in dict.items():
  149.        if k_perms==len(value):
  150.            final.append(key)
  151.  
  152.    return [len(final),min(final,default=0),max(final,default=0)]
  153.    #
  154.    # print(dict)
  155. print(permutational_prime(2000,1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement