Welcome, guest | Sign In | My Account | Store | Cart

A class Ive had in my snippets for awhile that can generate prime, perfect and fibonacci sequences as well as check whether or not a supplied value is any of them.

Python, 164 lines
  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
"""
A Fun Widget Class For Playing With Prime, Perfect and Fibonacci Numbers

"""

class PrimePerFib:
    
    def __init__(self):
        pass
        
  
    def Factor(self, n):
      yield 1  
      i = 2  
      limit = n**0.5  
      while i <= limit:  
        if n % i == 0:  
          yield i  
          n = n / i  
          limit = n**0.5  
        else:  
          i += 1  
      if n > 1:  
        yield n  
            
            
    def PrimeFactor(self, n):
        for x in self.Factor(n):
            if x != 1 and x != n:
                return False
        return True

    """
    A Hybrid Sieve of Atkin, without all the quadratics and keeping of sequences
    allows for generating blocks of primes
    """
    def PrimeGen(self, count, start):
        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.PrimeFactor(n):
                        c += 1
                        yield n
            n += 2
            
            
                    
    def PrimeGet(self, num):
        r = self.PrimeGen(1,num).next()
        return r
    
    
    def IsPrime(self, num):
        return (False,True)[self.PrimeGet(num)==num]
    
      
    def NextPrime(self, num):
        return (self.PrimeGet(num),self.PrimeGet(num+1))[self.IsPrime(num)]
        

    def PerfectGen(self, count, start=0):
        output = 0
        prime  = 0 
        while output < count:
            prime = self.NextPrime(prime)
            mPrime = 2**prime - 1

            if not self.IsPrime(mPrime):
                continue
            
            pN =(2**(prime-1))*(2**prime - 1)
            if pN >= start:
                output += 1
                yield pN
                
                    
    def PerfectGet(self, num):
        return self.PerfectGen(1,num).next()
    
    
    def IsPerfect(self, num):
        return (False,True)[self.PerfectGet(num)==num]
    
    
    def NextPerfect(self, num):
        return (self.PerfectGet(num),self.PerfectGet(num+1))[self.IsPerfect(num)]
                
    
    def FibonacciGen(self, count, start=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 FibonacciGet(self, num):
        return self.FibonacciGen(1,num).next()
    

    def IsFibonacci(self, num):
        return (False,True)[self.FibonacciGet(num)==num]

    
    def NextFibonacci(self, num):
        return (self.FibonacciGet(num),self.FibonacciGet(num+1))[self.IsFibonacci(num)]
       

if __name__ == '__main__':
    
    PPF = PrimePerFib()
    
    ##########################################################################
    #Prime Numbers
    
    print "What Prime Number Comes After 56? ",PPF.NextPrime(56)
    print "Is 333 A Prime Number? ",PPF.IsPrime(333)

    Primes = []
    for Prime in PPF.PrimeGen(10,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.NextPerfect(9685)
    print "Is 8128 A Perfect Number ",PPF.IsPerfect(8128)
    
    Perfects = []
    for Perfect in PPF.PerfectGen(8, 0):
        Perfects.append(Perfect)
        
    print "Generated Perfect Numbers ",Perfects,"\n\n"
    
    ##########################################################################
    #Fibonacci Numbers
    
    print "What is the Next Fibonacci Number After 5 ? ",PPF.NextFibonacci(5)
    print "Is 16 A Fibonacci Number? ",PPF.IsFibonacci(16)
    
    Fibonaccis = []
    for Fibonacci in PPF.FibonacciGen(42, 10):
        Fibonaccis.append(Fibonacci)
        
    print "Generated Fibonacci Numbers ",Fibonaccis
        
    
Created by AJ. Mayorga on Mon, 17 May 2010 (MIT)
Python recipes (4591)
AJ. Mayorga's recipes (7)
Code Samples (4)

Required Modules

  • (none specified)

Other Information and Tasks