11
11
from sys import version , version_info
12
12
from argparse import ArgumentParser
13
13
14
+ DEBUG = False
15
+ VERBOSE = False
16
+
14
17
NUMPY_ENABLED = True
15
18
try :
16
19
from numpy import ones , nonzero , __version__
17
- print ('Detected numpy version {__version__}' .format (** locals ()))
20
+ if VERBOSE : print ('Detected numpy version {__version__}' .format (** locals ()))
18
21
19
22
except ImportError :
20
23
print ('Numpy is not found! Finding primes will be slower!' )
21
24
NUMPY_ENABLED = False
22
25
23
- PRIME_TEST = True
24
- FACTOR_TEST = True
26
+ MPMATH_ENABLED = True
27
+ try :
28
+ from mpmath import cyclotomic , __version__
29
+ if VERBOSE : print ('Detected mpmath version {__version__}' .format (** locals ()))
30
+
31
+ except ImportError :
32
+ print ('Mpmath is not found! You can\' t find several kinds of primes!' )
33
+ MPMATH_ENABLED = False
34
+
35
+ PRIME_TEST = FACTOR_TEST = not DEBUG
36
+
25
37
try :
26
38
from rsa import newkeys , __version__
27
- print ('Detected rsa version {__version__}' .format (** locals ()))
39
+ if VERBOSE : print ('Detected rsa version {__version__}' .format (** locals ()))
28
40
29
41
except ImportError :
30
42
print ('Rsa is not found! Factor Test will be disabled!' )
31
43
FACTOR_TEST = False
32
44
33
- print ()
45
+ if VERBOSE : print ()
34
46
35
47
LEFT = 'left'
36
48
RIGHT = 'right'
@@ -88,9 +100,6 @@ def is_prime(n):
88
100
def all_primes (n , output = 'array' ):
89
101
'''
90
102
Return a prime list below n.
91
-
92
- Arguments:
93
- output ----- 'array' or 'list' ----- The output type of the function.
94
103
'''
95
104
_check_num (n )
96
105
if NUMPY_ENABLED :
@@ -114,6 +123,56 @@ def all_primes(n, output = 'array'):
114
123
else :
115
124
return [x for x in range (2 , n + 1 ) if sieve [x ]]
116
125
126
+ def get_repetend_length (denominator ):
127
+ '''
128
+ Return the length of the repetend of n / denominator.
129
+ '''
130
+ length = 1
131
+ if is_prime (denominator ):
132
+ while True :
133
+ if (10 ** length - 1 ) % denominator == 0 :
134
+ break
135
+
136
+ length += 1
137
+
138
+ else :
139
+ pass
140
+
141
+ return length
142
+ '''
143
+ output = ''
144
+ all_num = []
145
+ upload = []
146
+ con = 10 // denominator
147
+ r = 10 % denominator
148
+ upload.append(con)
149
+ upload.append(r)
150
+ all_num.append(upload)
151
+ while True:
152
+ con = r * 10 // denominator
153
+ r = r * 10 % denominator
154
+ upload = []
155
+ upload.append(con)
156
+ upload.append(r)
157
+ index1 = 0
158
+ index2 = 0
159
+ for x in all_num:
160
+ if x == upload:
161
+ for a in all_num:
162
+ if index1 == index2:
163
+ output += '['
164
+
165
+ output += str(a[0])
166
+ index2 += 1
167
+
168
+ output += ']'
169
+ return output.find(']') - output.find('[') - 1
170
+
171
+ index1 += 1
172
+
173
+ all_num.append(upload)
174
+ '''
175
+
117
176
class FactorError (Exception ):
118
177
pass
119
178
@@ -515,7 +574,6 @@ def find_leylands(n):
515
574
for y in range (2 , round (n ** 0.5 )):
516
575
if not y in all_x :
517
576
ans = x ** y + y ** x
518
- #ans = fast_pow(x, y) + fast_pow(y, x)
519
577
if ans < n and ans in primes :
520
578
leylands .append (ans )
521
579
@@ -534,7 +592,6 @@ def find_leylands_second_kind(n):
534
592
for y in range (2 , round (n ** 0.5 )):
535
593
if not y in all_x :
536
594
ans = x ** y - y ** x
537
- #ans = fast_pow(x, y) - fast_pow(y, x)
538
595
if ans < n and ans in primes :
539
596
leylands_second_kind .append (ans )
540
597
@@ -554,6 +611,52 @@ def find_woodalls(n):
554
611
555
612
return woodalls
556
613
614
+ def find_uniques (n ):
615
+ '''
616
+ Return a list that has all unique primes below n.
617
+ '''
618
+ _check_num (n )
619
+ primes = all_primes (n , output = 'list' )
620
+ try :
621
+ primes .pop (primes .index (2 ))
622
+ primes .pop (primes .index (5 ))
623
+
624
+ except ValueError :
625
+ pass
626
+
627
+ uniques = []
628
+ for x in primes :
629
+ length = get_repetend_length (x )
630
+ cyc = int (cyclotomic (length , 10 ))
631
+ ans = cyc // gcd (cyc , length )
632
+ c = log (ans , x )
633
+ if int (c ) == c :
634
+ uniques .append (x )
635
+
636
+ return uniques
637
+ from itertools import product , chain , permutations
638
+ def find_friedmans (n ):
639
+ '''
640
+ Return a list that has all friedman primes below n.
641
+ '''
642
+ _check_num (n )
643
+ primes = all_primes (n )
644
+ friedmans = []
645
+ r = {}
646
+ q = lambda n , h = 1 : ['(' + i + c + j + ')' for (i , j ), c in product (chain (* [product (* map (q , f )) for f in sum (([(x [:q ], x [q :]) for q in range (1 , len (x ))] for x in map ('' .join , permutations (n ))), [])]), list ('+-*/^' ) + ['' ] * h )] if 1 < len (n ) else [n ] * h
647
+ for i , j in chain (* [product (q (str (s ), 0 ), [s ]) for s in range (125 , n + 1 )]):
648
+ try :
649
+ exec ('if eval(%r) == j: r[j]=i' % i .replace ('^' , '**' ))
650
+
651
+ except Exception :
652
+ pass
653
+
654
+ for x , i in r .items ():
655
+ if x in primes :
656
+ friedmans .append (x )
657
+
658
+ return friedmans
659
+
557
660
def factor_siqs (n ):
558
661
'''
559
662
Return a list that has all factors of n.
@@ -3137,6 +3240,8 @@ def test():
3137
3240
print (f'Leyland Primes: { find_leylands (33000 )} \n ' )
3138
3241
print (f'Leyland Primes of the second kind: { find_leylands_second_kind (58050 )} \n ' )
3139
3242
print (f'Woodall Primes: { find_woodalls (400 )} \n ' )
3243
+ print (f'Unique Primes: { find_uniques (105 )} \n ' )
3244
+ print (f'Friedman Primes: { find_friedmans (128 )} \n ' )
3140
3245
end_tm = time ()
3141
3246
s = '\n ' if FACTOR_TEST else ''
3142
3247
print (f'Prime Test Time: { round (end_tm - start_tm , 12 )} seconds.' + s )
0 commit comments