Primenumber position search ends in too big scale what to do?
def isprime(number):
counter = 0
for n in range(1,number+1):
if number % n == 0:
counter += 1
if counter == 2:
return True
This is a function to check whether the number is prime at all.
my_list =
n1 = 1
while len(my_list) <= 10000:
n1 += 1
if isprime(n1) is True:
my_list.append(n1)
print(my_list[-1])
So this is my code for now, it works totally fine but is not optimized at all and I wanted to learn from bottom up how to make such a function faster, so that my computer is able to do the calculations.
I tried to find the 10001 prime number.
(started with zero so that is the reason for the <= 10000)
python function primes
add a comment |
def isprime(number):
counter = 0
for n in range(1,number+1):
if number % n == 0:
counter += 1
if counter == 2:
return True
This is a function to check whether the number is prime at all.
my_list =
n1 = 1
while len(my_list) <= 10000:
n1 += 1
if isprime(n1) is True:
my_list.append(n1)
print(my_list[-1])
So this is my code for now, it works totally fine but is not optimized at all and I wanted to learn from bottom up how to make such a function faster, so that my computer is able to do the calculations.
I tried to find the 10001 prime number.
(started with zero so that is the reason for the <= 10000)
python function primes
2
Firstly, you should check the counter in the loop and return false when the counter is greater than 2.
– Attila Bognár
Nov 26 '18 at 13:12
add a comment |
def isprime(number):
counter = 0
for n in range(1,number+1):
if number % n == 0:
counter += 1
if counter == 2:
return True
This is a function to check whether the number is prime at all.
my_list =
n1 = 1
while len(my_list) <= 10000:
n1 += 1
if isprime(n1) is True:
my_list.append(n1)
print(my_list[-1])
So this is my code for now, it works totally fine but is not optimized at all and I wanted to learn from bottom up how to make such a function faster, so that my computer is able to do the calculations.
I tried to find the 10001 prime number.
(started with zero so that is the reason for the <= 10000)
python function primes
def isprime(number):
counter = 0
for n in range(1,number+1):
if number % n == 0:
counter += 1
if counter == 2:
return True
This is a function to check whether the number is prime at all.
my_list =
n1 = 1
while len(my_list) <= 10000:
n1 += 1
if isprime(n1) is True:
my_list.append(n1)
print(my_list[-1])
So this is my code for now, it works totally fine but is not optimized at all and I wanted to learn from bottom up how to make such a function faster, so that my computer is able to do the calculations.
I tried to find the 10001 prime number.
(started with zero so that is the reason for the <= 10000)
python function primes
python function primes
edited Nov 26 '18 at 13:09
Some programmer dude
304k25265426
304k25265426
asked Nov 26 '18 at 13:08
Karim Abou El NagaKarim Abou El Naga
163
163
2
Firstly, you should check the counter in the loop and return false when the counter is greater than 2.
– Attila Bognár
Nov 26 '18 at 13:12
add a comment |
2
Firstly, you should check the counter in the loop and return false when the counter is greater than 2.
– Attila Bognár
Nov 26 '18 at 13:12
2
2
Firstly, you should check the counter in the loop and return false when the counter is greater than 2.
– Attila Bognár
Nov 26 '18 at 13:12
Firstly, you should check the counter in the loop and return false when the counter is greater than 2.
– Attila Bognár
Nov 26 '18 at 13:12
add a comment |
2 Answers
2
active
oldest
votes
For numbers the size you need to solve Project Euler #7, it is sufficient to determine primality by trial division. Here is a simple primality checker using a 2,3,5-wheel, which is about twice as fast as the naive primality checker posted by Yakov Dan:
def isPrime(n): # 2,3,5-wheel
ws = [1,2,2,4,2,4,2,4,6,2,6]
w, f = 0, 2
while f * f <= n:
if n % f == 0:
return False
f = f + ws[w]
w = w + 1
if w == 11: w = 3
return True
For larger numbers, it is better to use a Miller-Rabin primality checker:
def isPrime(n, k=5): # miller-rabin
from random import randint
if n < 2: return False
for p in [2,3,5,7,11,13,17,19,23,29]:
if n % p == 0: return n == p
s, d = 0, n-1
while d % 2 == 0:
s, d = s+1, d/2
for i in range(k):
x = pow(randint(2, n-1), d, n)
if x == 1 or x == n-1: continue
for r in range(1, s):
x = (x * x) % n
if x == 1: return False
if x == n-1: break
else: return False
return True
Either of those methods will be much slower than the Sieve of Eratosthenes, invented over two thousand years ago by a Greek mathematician:
def primes(n): # sieve of eratosthenes
i, p, ps, m = 0, 3, [2], n // 2
sieve = [True] * m
while p <= n:
if sieve[i]:
ps.append(p)
for j in range((p*p-3)/2, m, p):
sieve[j] = False
i, p = i+1, p+2
return ps
To solve Project Euler #7, call the sieve with n = 120000 and discard the excess primes. It will be more convenient for you to use a sieve in the form of a generator:
def primegen(start=0): # stackoverflow.com/a/20660551
if start <= 2: yield 2 # prime (!) the pump
if start <= 3: yield 3 # prime (!) the pump
ps = primegen() # sieving primes
p = next(ps) and next(ps) # first sieving prime
q = p * p; D = {} # initialization
def add(m, s): # insert multiple/stride
while m in D: m += s # find unused multiple
D[m] = s # save multiple/stride
while q <= start: # initialize multiples
x = (start // p) * p # first multiple of p
if x < start: x += p # must be >= start
if x % 2 == 0: x += p # ... and must be odd
add(x, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
c = max(start-2, 3) # first prime candidate
if c % 2 == 0: c += 1 # candidate must be odd
while True: # infinite list
c += 2 # next odd candidate
if c in D: # c is composite
s = D.pop(c) # fetch stride
add(c+s, s) # add next multiple
elif c < q: yield c # c is prime; yield it
else: # (c == q) # add p to sieve
add(c+p+p, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
I discuss all these things at my blog.
good to see you back!
– James K Polk
Nov 26 '18 at 19:03
add a comment |
Fast primality tests are a huge subject.
What is often fast enough is to check if a number is divisible by two or by three, and then to check all possible divisors from 4 to the square root of the number.
So, try this:
def is_prime(n):
if n == 1:
return False
if n == 2:
return True
if n == 3:
return True
if n % 2 == 0
return False
if n % 3 == 0
return False
for d in range(5, int(n**0.5)+1,2):
if n % d == 0
return False
return True
Thank you, it's a nice trick.
– Yakov Dan
Nov 26 '18 at 19:10
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481831%2fprimenumber-position-search-ends-in-too-big-scale-what-to-do%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For numbers the size you need to solve Project Euler #7, it is sufficient to determine primality by trial division. Here is a simple primality checker using a 2,3,5-wheel, which is about twice as fast as the naive primality checker posted by Yakov Dan:
def isPrime(n): # 2,3,5-wheel
ws = [1,2,2,4,2,4,2,4,6,2,6]
w, f = 0, 2
while f * f <= n:
if n % f == 0:
return False
f = f + ws[w]
w = w + 1
if w == 11: w = 3
return True
For larger numbers, it is better to use a Miller-Rabin primality checker:
def isPrime(n, k=5): # miller-rabin
from random import randint
if n < 2: return False
for p in [2,3,5,7,11,13,17,19,23,29]:
if n % p == 0: return n == p
s, d = 0, n-1
while d % 2 == 0:
s, d = s+1, d/2
for i in range(k):
x = pow(randint(2, n-1), d, n)
if x == 1 or x == n-1: continue
for r in range(1, s):
x = (x * x) % n
if x == 1: return False
if x == n-1: break
else: return False
return True
Either of those methods will be much slower than the Sieve of Eratosthenes, invented over two thousand years ago by a Greek mathematician:
def primes(n): # sieve of eratosthenes
i, p, ps, m = 0, 3, [2], n // 2
sieve = [True] * m
while p <= n:
if sieve[i]:
ps.append(p)
for j in range((p*p-3)/2, m, p):
sieve[j] = False
i, p = i+1, p+2
return ps
To solve Project Euler #7, call the sieve with n = 120000 and discard the excess primes. It will be more convenient for you to use a sieve in the form of a generator:
def primegen(start=0): # stackoverflow.com/a/20660551
if start <= 2: yield 2 # prime (!) the pump
if start <= 3: yield 3 # prime (!) the pump
ps = primegen() # sieving primes
p = next(ps) and next(ps) # first sieving prime
q = p * p; D = {} # initialization
def add(m, s): # insert multiple/stride
while m in D: m += s # find unused multiple
D[m] = s # save multiple/stride
while q <= start: # initialize multiples
x = (start // p) * p # first multiple of p
if x < start: x += p # must be >= start
if x % 2 == 0: x += p # ... and must be odd
add(x, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
c = max(start-2, 3) # first prime candidate
if c % 2 == 0: c += 1 # candidate must be odd
while True: # infinite list
c += 2 # next odd candidate
if c in D: # c is composite
s = D.pop(c) # fetch stride
add(c+s, s) # add next multiple
elif c < q: yield c # c is prime; yield it
else: # (c == q) # add p to sieve
add(c+p+p, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
I discuss all these things at my blog.
good to see you back!
– James K Polk
Nov 26 '18 at 19:03
add a comment |
For numbers the size you need to solve Project Euler #7, it is sufficient to determine primality by trial division. Here is a simple primality checker using a 2,3,5-wheel, which is about twice as fast as the naive primality checker posted by Yakov Dan:
def isPrime(n): # 2,3,5-wheel
ws = [1,2,2,4,2,4,2,4,6,2,6]
w, f = 0, 2
while f * f <= n:
if n % f == 0:
return False
f = f + ws[w]
w = w + 1
if w == 11: w = 3
return True
For larger numbers, it is better to use a Miller-Rabin primality checker:
def isPrime(n, k=5): # miller-rabin
from random import randint
if n < 2: return False
for p in [2,3,5,7,11,13,17,19,23,29]:
if n % p == 0: return n == p
s, d = 0, n-1
while d % 2 == 0:
s, d = s+1, d/2
for i in range(k):
x = pow(randint(2, n-1), d, n)
if x == 1 or x == n-1: continue
for r in range(1, s):
x = (x * x) % n
if x == 1: return False
if x == n-1: break
else: return False
return True
Either of those methods will be much slower than the Sieve of Eratosthenes, invented over two thousand years ago by a Greek mathematician:
def primes(n): # sieve of eratosthenes
i, p, ps, m = 0, 3, [2], n // 2
sieve = [True] * m
while p <= n:
if sieve[i]:
ps.append(p)
for j in range((p*p-3)/2, m, p):
sieve[j] = False
i, p = i+1, p+2
return ps
To solve Project Euler #7, call the sieve with n = 120000 and discard the excess primes. It will be more convenient for you to use a sieve in the form of a generator:
def primegen(start=0): # stackoverflow.com/a/20660551
if start <= 2: yield 2 # prime (!) the pump
if start <= 3: yield 3 # prime (!) the pump
ps = primegen() # sieving primes
p = next(ps) and next(ps) # first sieving prime
q = p * p; D = {} # initialization
def add(m, s): # insert multiple/stride
while m in D: m += s # find unused multiple
D[m] = s # save multiple/stride
while q <= start: # initialize multiples
x = (start // p) * p # first multiple of p
if x < start: x += p # must be >= start
if x % 2 == 0: x += p # ... and must be odd
add(x, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
c = max(start-2, 3) # first prime candidate
if c % 2 == 0: c += 1 # candidate must be odd
while True: # infinite list
c += 2 # next odd candidate
if c in D: # c is composite
s = D.pop(c) # fetch stride
add(c+s, s) # add next multiple
elif c < q: yield c # c is prime; yield it
else: # (c == q) # add p to sieve
add(c+p+p, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
I discuss all these things at my blog.
good to see you back!
– James K Polk
Nov 26 '18 at 19:03
add a comment |
For numbers the size you need to solve Project Euler #7, it is sufficient to determine primality by trial division. Here is a simple primality checker using a 2,3,5-wheel, which is about twice as fast as the naive primality checker posted by Yakov Dan:
def isPrime(n): # 2,3,5-wheel
ws = [1,2,2,4,2,4,2,4,6,2,6]
w, f = 0, 2
while f * f <= n:
if n % f == 0:
return False
f = f + ws[w]
w = w + 1
if w == 11: w = 3
return True
For larger numbers, it is better to use a Miller-Rabin primality checker:
def isPrime(n, k=5): # miller-rabin
from random import randint
if n < 2: return False
for p in [2,3,5,7,11,13,17,19,23,29]:
if n % p == 0: return n == p
s, d = 0, n-1
while d % 2 == 0:
s, d = s+1, d/2
for i in range(k):
x = pow(randint(2, n-1), d, n)
if x == 1 or x == n-1: continue
for r in range(1, s):
x = (x * x) % n
if x == 1: return False
if x == n-1: break
else: return False
return True
Either of those methods will be much slower than the Sieve of Eratosthenes, invented over two thousand years ago by a Greek mathematician:
def primes(n): # sieve of eratosthenes
i, p, ps, m = 0, 3, [2], n // 2
sieve = [True] * m
while p <= n:
if sieve[i]:
ps.append(p)
for j in range((p*p-3)/2, m, p):
sieve[j] = False
i, p = i+1, p+2
return ps
To solve Project Euler #7, call the sieve with n = 120000 and discard the excess primes. It will be more convenient for you to use a sieve in the form of a generator:
def primegen(start=0): # stackoverflow.com/a/20660551
if start <= 2: yield 2 # prime (!) the pump
if start <= 3: yield 3 # prime (!) the pump
ps = primegen() # sieving primes
p = next(ps) and next(ps) # first sieving prime
q = p * p; D = {} # initialization
def add(m, s): # insert multiple/stride
while m in D: m += s # find unused multiple
D[m] = s # save multiple/stride
while q <= start: # initialize multiples
x = (start // p) * p # first multiple of p
if x < start: x += p # must be >= start
if x % 2 == 0: x += p # ... and must be odd
add(x, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
c = max(start-2, 3) # first prime candidate
if c % 2 == 0: c += 1 # candidate must be odd
while True: # infinite list
c += 2 # next odd candidate
if c in D: # c is composite
s = D.pop(c) # fetch stride
add(c+s, s) # add next multiple
elif c < q: yield c # c is prime; yield it
else: # (c == q) # add p to sieve
add(c+p+p, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
I discuss all these things at my blog.
For numbers the size you need to solve Project Euler #7, it is sufficient to determine primality by trial division. Here is a simple primality checker using a 2,3,5-wheel, which is about twice as fast as the naive primality checker posted by Yakov Dan:
def isPrime(n): # 2,3,5-wheel
ws = [1,2,2,4,2,4,2,4,6,2,6]
w, f = 0, 2
while f * f <= n:
if n % f == 0:
return False
f = f + ws[w]
w = w + 1
if w == 11: w = 3
return True
For larger numbers, it is better to use a Miller-Rabin primality checker:
def isPrime(n, k=5): # miller-rabin
from random import randint
if n < 2: return False
for p in [2,3,5,7,11,13,17,19,23,29]:
if n % p == 0: return n == p
s, d = 0, n-1
while d % 2 == 0:
s, d = s+1, d/2
for i in range(k):
x = pow(randint(2, n-1), d, n)
if x == 1 or x == n-1: continue
for r in range(1, s):
x = (x * x) % n
if x == 1: return False
if x == n-1: break
else: return False
return True
Either of those methods will be much slower than the Sieve of Eratosthenes, invented over two thousand years ago by a Greek mathematician:
def primes(n): # sieve of eratosthenes
i, p, ps, m = 0, 3, [2], n // 2
sieve = [True] * m
while p <= n:
if sieve[i]:
ps.append(p)
for j in range((p*p-3)/2, m, p):
sieve[j] = False
i, p = i+1, p+2
return ps
To solve Project Euler #7, call the sieve with n = 120000 and discard the excess primes. It will be more convenient for you to use a sieve in the form of a generator:
def primegen(start=0): # stackoverflow.com/a/20660551
if start <= 2: yield 2 # prime (!) the pump
if start <= 3: yield 3 # prime (!) the pump
ps = primegen() # sieving primes
p = next(ps) and next(ps) # first sieving prime
q = p * p; D = {} # initialization
def add(m, s): # insert multiple/stride
while m in D: m += s # find unused multiple
D[m] = s # save multiple/stride
while q <= start: # initialize multiples
x = (start // p) * p # first multiple of p
if x < start: x += p # must be >= start
if x % 2 == 0: x += p # ... and must be odd
add(x, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
c = max(start-2, 3) # first prime candidate
if c % 2 == 0: c += 1 # candidate must be odd
while True: # infinite list
c += 2 # next odd candidate
if c in D: # c is composite
s = D.pop(c) # fetch stride
add(c+s, s) # add next multiple
elif c < q: yield c # c is prime; yield it
else: # (c == q) # add p to sieve
add(c+p+p, p+p) # insert in sieve
p = next(ps) # next sieving prime
q = p * p # ... and its square
I discuss all these things at my blog.
answered Nov 26 '18 at 14:33
user448810user448810
14.2k22443
14.2k22443
good to see you back!
– James K Polk
Nov 26 '18 at 19:03
add a comment |
good to see you back!
– James K Polk
Nov 26 '18 at 19:03
good to see you back!
– James K Polk
Nov 26 '18 at 19:03
good to see you back!
– James K Polk
Nov 26 '18 at 19:03
add a comment |
Fast primality tests are a huge subject.
What is often fast enough is to check if a number is divisible by two or by three, and then to check all possible divisors from 4 to the square root of the number.
So, try this:
def is_prime(n):
if n == 1:
return False
if n == 2:
return True
if n == 3:
return True
if n % 2 == 0
return False
if n % 3 == 0
return False
for d in range(5, int(n**0.5)+1,2):
if n % d == 0
return False
return True
Thank you, it's a nice trick.
– Yakov Dan
Nov 26 '18 at 19:10
add a comment |
Fast primality tests are a huge subject.
What is often fast enough is to check if a number is divisible by two or by three, and then to check all possible divisors from 4 to the square root of the number.
So, try this:
def is_prime(n):
if n == 1:
return False
if n == 2:
return True
if n == 3:
return True
if n % 2 == 0
return False
if n % 3 == 0
return False
for d in range(5, int(n**0.5)+1,2):
if n % d == 0
return False
return True
Thank you, it's a nice trick.
– Yakov Dan
Nov 26 '18 at 19:10
add a comment |
Fast primality tests are a huge subject.
What is often fast enough is to check if a number is divisible by two or by three, and then to check all possible divisors from 4 to the square root of the number.
So, try this:
def is_prime(n):
if n == 1:
return False
if n == 2:
return True
if n == 3:
return True
if n % 2 == 0
return False
if n % 3 == 0
return False
for d in range(5, int(n**0.5)+1,2):
if n % d == 0
return False
return True
Fast primality tests are a huge subject.
What is often fast enough is to check if a number is divisible by two or by three, and then to check all possible divisors from 4 to the square root of the number.
So, try this:
def is_prime(n):
if n == 1:
return False
if n == 2:
return True
if n == 3:
return True
if n % 2 == 0
return False
if n % 3 == 0
return False
for d in range(5, int(n**0.5)+1,2):
if n % d == 0
return False
return True
edited Nov 26 '18 at 19:09
answered Nov 26 '18 at 13:23
Yakov DanYakov Dan
1,401718
1,401718
Thank you, it's a nice trick.
– Yakov Dan
Nov 26 '18 at 19:10
add a comment |
Thank you, it's a nice trick.
– Yakov Dan
Nov 26 '18 at 19:10
Thank you, it's a nice trick.
– Yakov Dan
Nov 26 '18 at 19:10
Thank you, it's a nice trick.
– Yakov Dan
Nov 26 '18 at 19:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481831%2fprimenumber-position-search-ends-in-too-big-scale-what-to-do%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Firstly, you should check the counter in the loop and return false when the counter is greater than 2.
– Attila Bognár
Nov 26 '18 at 13:12