-
Notifications
You must be signed in to change notification settings - Fork 8
/
primeperfib.py
170 lines (131 loc) · 5.31 KB
/
primeperfib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
This is an adaptation and annotated version of code originally found here:
http://code.activestate.com/recipes/577229-prime-perfect-and-fibonacci-number-widget-class/
"""
class PrimePerFib:
"""Generate and manipulate Prime, Perfect and Fibonacci Numbers"""
def __init__(self):
pass
def gen_factor(self, n):
"""Generate all the factors of a number"""
l1, l2 = [], []
for i in range(1, int(n ** 0.5) + 1):
q,r = n//i, n%i
if r == 0:
l1.append(i)
l2.append(q)
if l1[-1] == l2[-1]:
l1.pop()
l2.reverse()
for prime in (l1 + l2):
yield prime
def is_prime(self, n):
for x in self.gen_factor(n):
if x != 1 and x != n:
return False
return True
def prime_gen(self, count, start):
"""Generate a sequence of prime numbers
Arguments:
count: The number of prime numbers to generate
start: The start of the sequence
"""
c = 0
base = [2,3,5]
start = int(round(start))
n = (2,max(2,(start,start+1)[start % 2 == 0]))[start != 2]
while c < count:
if n in base:
yield n
if n == 2: n+=1; c+=1; continue
else: n+=2; c+=1; continue
if not [m for m in base if (n%60) % m == 0 ]:
if n % n**0.5 != 0:
if self.is_prime(n):
c += 1
yield n
n += 2
def prime_get(self, num):
"""Get the next prime number starting at and incluing the input value"""
r = self.prime_gen(count=1,start=num).next()
return r
def prime_next(self, num):
"""Get the next prime number starting at and not incluing the input value"""
return self.prime_get(num+1)
def perfect_gen(self, count, start=0):
"""Generate a sequence of perfect numbers
Arguments:
count: The number of perfect numbers to generate
start: The start of the sequence, defaults to 0
"""
output = 0
prime = 0
while output < count:
prime = self.prime_next(prime)
mPrime = 2**prime - 1
if not self.is_prime(mPrime):
continue
pN =(2**(prime-1))*(2**prime - 1)
if pN >= start:
output += 1
yield pN
def perfect_get(self, num):
"""Get the next perfect number starting at and incluing the input value"""
return self.perfect_gen(count=1,start=num).next()
def is_perfect(self, num):
"""Check if the input value is a perfect number"""
return self.perfect_get(num)==num
def perfect_next(self, num):
"""Get the next perfect number starting at and not incluing the input value"""
return self.perfect_get(num+1)
def fibonacci_gen(self, count, start=0):
"""Generate a sequence of fibonacci numbers
Arguments:
count: The number of fibonacci numbers to generate
start: The start of the sequence, defaults to 0
"""
output = 0
fib = [0,1]
while output < count:
fN = fib[len(fib)-1] + fib[len(fib)-2]
fib.append(fN)
fib.pop(0)
if fN >= start:
output += 1
yield fN
def fibonacci_get(self, num):
"""Get the next fibonacci number starting at and incluing the input value"""
return self.fibonacci_gen(count=1,start=num).next()
def is_fibonacci(self, num):
"""Check if the input value is a fibonacci number"""
return self.fibonacci_get(num)==num
def fibonacci_next(self, num):
"""Get the next fibonacci number starting at and not incluing the input value"""
return self.fibonacci_get(num+1)
if __name__ == '__main__':
PPF = PrimePerFib()
##########################################################################
#Prime Numbers
print "What Prime Number Comes After 56? ",PPF.prime_next(56)
print "Is 333 A Prime Number? ",PPF.is_prime(333)
Primes = []
for Prime in PPF.prime_gen(count=10,start=42):
Primes.append(Prime)
print "Generated Primes: ",Primes,"\n\n"
##########################################################################
#Perfect Numbers
#Need Some Horsepower In Your Machine To Play With These
print "What Perfect Number Comes After 9685 ",PPF.perfect_next(9685)
print "Is 8128 A Perfect Number ",PPF.is_perfect(8128)
Perfects = []
for Perfect in PPF.perfect_gen(count=8, start=0):
Perfects.append(Perfect)
print "Generated Perfect Numbers ",Perfects,"\n\n"
##########################################################################
#Fibonacci Numbers
print "What is the Next Fibonacci Number After 5 ? ",PPF.fibonacci_next(5)
print "Is 16 A Fibonacci Number? ",PPF.is_fibonacci(16)
Fibonaccis = []
for Fibonacci in PPF.fibonacci_gen(count=42, start=10):
Fibonaccis.append(Fibonacci)
print "Generated Fibonacci Numbers ",Fibonaccis