Skip to main content
Commonmark migration
Source Link

Disclaimer: I'm not a Python expert

#Bug

Bug

As is, your code has a bug.

Enter lower range: 2   
Enter upper range: 10
Running...
Finished...
Sum of prime numbers: 19
List of prime numbers: [2, 2, 3, 5, 7]
Number of prime numbers: 5

To fix this bug, change

if num == 2:

to

elif num == 2:

Also, it's not really a bug, but this takes a really, really long time to run for max = 3,000,000,000. I can't get it to finish even 1/1000 of that...

#Other Stuff

Other Stuff

Include a shebang line to clarify how you want your code to be interpreted (which environment).

Your variable names are descriptive. There's no need to comment things that repeat the variable names.

Your algorithm is sort of slow. The classic Sieve of Eratosthenes is probably your best bet. Be careful how you implement it -- you can spend a lot of time re-testing values.

#Result

Result

After making the changes, this is what I ended up with

#!/usr/bin/env python

lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))

print "Running..."

sieve = list(range(upper + 1))
length = len(sieve)

# Keeping track of the primes will save time later

primes = []


current = 2
while current < length:
  primes.append(current)
  index = current * 2
  while index < length:
    sieve[index] = 0
    index = index + current
  current = current + 1
  # do not waste time with multiples
  while current < length and not sieve[current]:
    current = current + 1

# enforce the lower bound
while primes[0] < lower:
  primes.pop(0)

print "Finished..."
print "Sum of prime numbers:", sum(primes)
print "List of prime numbers:", primes
print "Number of prime numbers:", len(primes)

This could be improved to only check odd numbers (or only check non-multiples of three, etc.), but I'll leave that for you.

#Final note

Final note

If you really want to compute primes over 3bn, I wouldn't recommend using Python. I would recommend using a language with less overhead (maybe C?) and caching the first 1000 or so primes. When you run your sieve, print out each new prime as it is found, and just run your program with the biggest max you can fit. The program might not terminate, but you'll get some big primes...

Disclaimer: I'm not a Python expert

#Bug

As is, your code has a bug.

Enter lower range: 2   
Enter upper range: 10
Running...
Finished...
Sum of prime numbers: 19
List of prime numbers: [2, 2, 3, 5, 7]
Number of prime numbers: 5

To fix this bug, change

if num == 2:

to

elif num == 2:

Also, it's not really a bug, but this takes a really, really long time to run for max = 3,000,000,000. I can't get it to finish even 1/1000 of that...

#Other Stuff

Include a shebang line to clarify how you want your code to be interpreted (which environment).

Your variable names are descriptive. There's no need to comment things that repeat the variable names.

Your algorithm is sort of slow. The classic Sieve of Eratosthenes is probably your best bet. Be careful how you implement it -- you can spend a lot of time re-testing values.

#Result

After making the changes, this is what I ended up with

#!/usr/bin/env python

lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))

print "Running..."

sieve = list(range(upper + 1))
length = len(sieve)

# Keeping track of the primes will save time later

primes = []


current = 2
while current < length:
  primes.append(current)
  index = current * 2
  while index < length:
    sieve[index] = 0
    index = index + current
  current = current + 1
  # do not waste time with multiples
  while current < length and not sieve[current]:
    current = current + 1

# enforce the lower bound
while primes[0] < lower:
  primes.pop(0)

print "Finished..."
print "Sum of prime numbers:", sum(primes)
print "List of prime numbers:", primes
print "Number of prime numbers:", len(primes)

This could be improved to only check odd numbers (or only check non-multiples of three, etc.), but I'll leave that for you.

#Final note

If you really want to compute primes over 3bn, I wouldn't recommend using Python. I would recommend using a language with less overhead (maybe C?) and caching the first 1000 or so primes. When you run your sieve, print out each new prime as it is found, and just run your program with the biggest max you can fit. The program might not terminate, but you'll get some big primes...

Disclaimer: I'm not a Python expert

Bug

As is, your code has a bug.

Enter lower range: 2   
Enter upper range: 10
Running...
Finished...
Sum of prime numbers: 19
List of prime numbers: [2, 2, 3, 5, 7]
Number of prime numbers: 5

To fix this bug, change

if num == 2:

to

elif num == 2:

Also, it's not really a bug, but this takes a really, really long time to run for max = 3,000,000,000. I can't get it to finish even 1/1000 of that...

Other Stuff

Include a shebang line to clarify how you want your code to be interpreted (which environment).

Your variable names are descriptive. There's no need to comment things that repeat the variable names.

Your algorithm is sort of slow. The classic Sieve of Eratosthenes is probably your best bet. Be careful how you implement it -- you can spend a lot of time re-testing values.

Result

After making the changes, this is what I ended up with

#!/usr/bin/env python

lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))

print "Running..."

sieve = list(range(upper + 1))
length = len(sieve)

# Keeping track of the primes will save time later

primes = []


current = 2
while current < length:
  primes.append(current)
  index = current * 2
  while index < length:
    sieve[index] = 0
    index = index + current
  current = current + 1
  # do not waste time with multiples
  while current < length and not sieve[current]:
    current = current + 1

# enforce the lower bound
while primes[0] < lower:
  primes.pop(0)

print "Finished..."
print "Sum of prime numbers:", sum(primes)
print "List of prime numbers:", primes
print "Number of prime numbers:", len(primes)

This could be improved to only check odd numbers (or only check non-multiples of three, etc.), but I'll leave that for you.

Final note

If you really want to compute primes over 3bn, I wouldn't recommend using Python. I would recommend using a language with less overhead (maybe C?) and caching the first 1000 or so primes. When you run your sieve, print out each new prime as it is found, and just run your program with the biggest max you can fit. The program might not terminate, but you'll get some big primes...

Source Link

Disclaimer: I'm not a Python expert

#Bug

As is, your code has a bug.

Enter lower range: 2   
Enter upper range: 10
Running...
Finished...
Sum of prime numbers: 19
List of prime numbers: [2, 2, 3, 5, 7]
Number of prime numbers: 5

To fix this bug, change

if num == 2:

to

elif num == 2:

Also, it's not really a bug, but this takes a really, really long time to run for max = 3,000,000,000. I can't get it to finish even 1/1000 of that...

#Other Stuff

Include a shebang line to clarify how you want your code to be interpreted (which environment).

Your variable names are descriptive. There's no need to comment things that repeat the variable names.

Your algorithm is sort of slow. The classic Sieve of Eratosthenes is probably your best bet. Be careful how you implement it -- you can spend a lot of time re-testing values.

#Result

After making the changes, this is what I ended up with

#!/usr/bin/env python

lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))

print "Running..."

sieve = list(range(upper + 1))
length = len(sieve)

# Keeping track of the primes will save time later

primes = []


current = 2
while current < length:
  primes.append(current)
  index = current * 2
  while index < length:
    sieve[index] = 0
    index = index + current
  current = current + 1
  # do not waste time with multiples
  while current < length and not sieve[current]:
    current = current + 1

# enforce the lower bound
while primes[0] < lower:
  primes.pop(0)

print "Finished..."
print "Sum of prime numbers:", sum(primes)
print "List of prime numbers:", primes
print "Number of prime numbers:", len(primes)

This could be improved to only check odd numbers (or only check non-multiples of three, etc.), but I'll leave that for you.

#Final note

If you really want to compute primes over 3bn, I wouldn't recommend using Python. I would recommend using a language with less overhead (maybe C?) and caching the first 1000 or so primes. When you run your sieve, print out each new prime as it is found, and just run your program with the biggest max you can fit. The program might not terminate, but you'll get some big primes...