0

I'm not a programmer, but I'm learning,

Can someone help me understand what FFT is? How do I implement it in a python code, for example say a python code to generate primes from 1 to 10000?

Also somebody suggested NumPy, I downloaded it and installed it, but dont know what it does. Is it related to FFT?

Any help would be appreciated.

Regards, babsdoc

babsdoc
  • 599
  • 2
  • 7
  • 21

2 Answers2

1

Ok, I'm going to ignore all that about the fft, (which is related to signal processing and not prime numbers at all).

The most famous basic and famous approach is the seive of Eratosthenes. Wikipedia has a nice visual example of how it works.

In (untested) Python you'd code it as follows:

max_val = 1000
seive = range(max_val)
seive[0] = None  # 1 is not prime!
for x in range(1, max_val):
    if seive[x]:
        print "{prime} is prime.".format(prime = x)
        # now remove multiples of prime x from the seive
        y = 2*x 
        while y <= max_val:
            seive[y] = None
            y = y + x
Codie CodeMonkey
  • 6,899
  • 2
  • 25
  • 43
0

Using the suggested function from here "Python: Checking if a number is prime"

I suggest following code for you:

def isprime(number):  
if number<=1:  
    return 0  
check=2  
maxneeded=number  
while check<maxneeded+1:  
    maxneeded=number/check  
    if number%check==0:  
        return 0  
    check+=1  
return 1 
prime_list = [i for i in xrange(1,1001) if isprime(i)]
print prime_list

>>> [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, ...

Adjust the xrange(1,1001) to your desired range and you get all prime numbers

mawueth
  • 2,368
  • 1
  • 11
  • 12