Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Memory Error in Primes

$
0
0
So I'm writing some code for a little prime number theory project of mine and I keep dealing with a memory error at some upper bound for some input value. I'm wondering if there might be a means to circumventing the problem and allowing the program to continue its computation, even if it takes more memory/time/etc. than what is normally allowed for?

My error:
>>> x.getDPF(987654321)

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    x.getDPF(987654321)
  File "C:\Python27\projects\primes.py", line 87, in getDPF
    if self.ifPrime(val) == 1:
  File "C:\Python27\projects\primes.py", line 42, in ifPrime
    for inc in range(1,val):
MemoryError



but is no problem if I do this:
>>> x.getDPF(123456789)
[[3, 2], [3607, 1], [3803, 1]]



The getGCD and ifPrime block (lines 31 to 45):
    def GCD(self, alpha, beta):
        if beta == 0:
            return alpha
        else:
            return self.GCD(beta, alpha % beta)

    def ifPrime(self, val):
    #checks if prime
        
        if val <= 1:
            return 0
        for inc in range(1,val):
            if self.GCD(val, inc) != 1:
                return 0
        return 1



and the getDPF or distinct prime factors block (lines 82 to 102):
    def getDPF(self, val):
    #gives distinct prime factors: i.e. 12546 -> [[2,1][3,2][17,1][41,1]]

        self.list_dist = self.clearList(self.list_dist)

        if self.ifPrime(val) == 1:
            return [[val,1]]
        else:
            for i in range(len(self.getPrimeFactor(val))):
                self.list_dist.append([0])
                self.list_dist[i][0] = self.list_pfactor[i]
                self.list_dist[i].append(1)

            for j in range(len(self.list_pfactor)):
                incr = 0
                while float(val)/self.list_pfactor[j] == val/self.list_pfactor[j]:
                    val /= self.list_pfactor[j]
                    incr += 1
                self.list_dist[j][1] = incr     
                
            return self.list_dist



Any useful tidbits?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>