1111from sys import version , version_info
1212from argparse import ArgumentParser
1313
14+ DEBUG = False
15+ VERBOSE = False
16+
1417NUMPY_ENABLED = True
1518try :
1619 from numpy import ones , nonzero , __version__
17- print ('Detected numpy version {__version__}' .format (** locals ()))
20+ if VERBOSE : print ('Detected numpy version {__version__}' .format (** locals ()))
1821
1922except ImportError :
2023 print ('Numpy is not found! Finding primes will be slower!' )
2124 NUMPY_ENABLED = False
2225
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+
2537try :
2638 from rsa import newkeys , __version__
27- print ('Detected rsa version {__version__}' .format (** locals ()))
39+ if VERBOSE : print ('Detected rsa version {__version__}' .format (** locals ()))
2840
2941except ImportError :
3042 print ('Rsa is not found! Factor Test will be disabled!' )
3143 FACTOR_TEST = False
3244
33- print ()
45+ if VERBOSE : print ()
3446
3547LEFT = 'left'
3648RIGHT = 'right'
@@ -88,9 +100,6 @@ def is_prime(n):
88100def all_primes (n , output = 'array' ):
89101 '''
90102 Return a prime list below n.
91-
92- Arguments:
93- output ----- 'array' or 'list' ----- The output type of the function.
94103 '''
95104 _check_num (n )
96105 if NUMPY_ENABLED :
@@ -114,6 +123,56 @@ def all_primes(n, output = 'array'):
114123 else :
115124 return [x for x in range (2 , n + 1 ) if sieve [x ]]
116125
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+
117176class FactorError (Exception ):
118177 pass
119178
@@ -515,7 +574,6 @@ def find_leylands(n):
515574 for y in range (2 , round (n ** 0.5 )):
516575 if not y in all_x :
517576 ans = x ** y + y ** x
518- #ans = fast_pow(x, y) + fast_pow(y, x)
519577 if ans < n and ans in primes :
520578 leylands .append (ans )
521579
@@ -534,7 +592,6 @@ def find_leylands_second_kind(n):
534592 for y in range (2 , round (n ** 0.5 )):
535593 if not y in all_x :
536594 ans = x ** y - y ** x
537- #ans = fast_pow(x, y) - fast_pow(y, x)
538595 if ans < n and ans in primes :
539596 leylands_second_kind .append (ans )
540597
@@ -554,6 +611,52 @@ def find_woodalls(n):
554611
555612 return woodalls
556613
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+
557660def factor_siqs (n ):
558661 '''
559662 Return a list that has all factors of n.
@@ -3137,6 +3240,8 @@ def test():
31373240 print (f'Leyland Primes: { find_leylands (33000 )} \n ' )
31383241 print (f'Leyland Primes of the second kind: { find_leylands_second_kind (58050 )} \n ' )
31393242 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 ' )
31403245 end_tm = time ()
31413246 s = '\n ' if FACTOR_TEST else ''
31423247 print (f'Prime Test Time: { round (end_tm - start_tm , 12 )} seconds.' + s )
0 commit comments