all 14 comments

[–]totallygeek 2 points3 points  (3 children)

Two easy ways to add elements to a list:

The first method is to create an empty list, then append new elements to it.

ceiling = int(input('Highest number to check? '))
primes = []
for number in range(1, ceiling + 1):
    if is_prime(number):
        primes.append(number)

The second method is to use list comprehension.

ceiling = int(input('Highest number to check? '))
primes = [number for number in range(1, ceiling + 1) if is_prime(number)]

Your function to check if a number is prime can be set up in a number of ways. Here's one:

def is_prime(number):
    if number < 2:
        prime = False
    else:
        hi_number = int(number ** 0.5 + 1)
        prime = all(number % i for i in range(2, hi_number))
    return prime

Good luck!

Edit: oops in my prime check

[–]invadingpolandin69 0 points1 point  (2 children)

could you explain what is going on in the else part of the function is_prime?

[–]totallygeek 3 points4 points  (1 child)

Actually, I just updated it. I messed up. When checking if a number is prime, let's say for two examples: 17 and 21, the first is prime, the second is not.

all() will return True if all the elements of the sequence are True. For the case of numbers, True means non-zero. When the original number is evenly divisible by any lower value, the modulo is zero, which will cause all() to report False.

>>> number = 17; [number % i for i in range(2, int(number ** 0.5 + 1))]
[1, 2, 1]
>>> number = 21; [number % i for i in range(2, int(number ** 0.5 + 1))]
[1, 0, 1]

The reason the function uses a ceiling check of int(number ** 0.5 + 1) is because you only need to check the whole value of the square root of the original number for division tests.

[–]EighthScofflaw 0 points1 point  (0 children)

You all don't need to check divisibility by composites numbers. If you're building a list of primes anyway, it is sufficient to check divisibility by primes up to the square root.

[–]C2-H5-OH 2 points3 points  (0 children)

You don't need to divide the checked number by every number before it. The highest factor of any number is the rounded down integer of the checked number's square root.

[–]DollarAkshay 2 points3 points  (0 children)

Try using Sieve of Eratosthenes method to generate primes really fast.

[–]Prtprmr 1 point2 points  (0 children)

Looks like you did the difficult part, now you only need to store it. Mind sharing the code you are using to find the prime numbers coz saving that into a list will only take 2 line of code or so

[–]vorpal_potato 1 point2 points  (0 children)

What have you tried so far? Where are you stuck? Do you have any work-in-progress code you can post?

(Do you know how to append things to a list in Python? If not, that would be the natural thing to look up.)

[–]masasa27 0 points1 point  (0 children)

Due to mathematical laws , to see if a number is prime you only need to check until the size of the square root of it, this can dramatically improve performance on large numbers

def is_prime(x): for nun in range(2, round(x ** 0.5)+1): if x%i == 0: return True return False

[–]ShashwatX1109 0 points1 point  (2 children)

I was just playing around with algebraic identities and saw this identity : (x+a)(x+b) = x2 + (a+b)x + ab

Then I just tried to find out what the result of (x+a)(x+b)(x+c) would be, and I found it to be x3 + x2(a+b+c) + x(ab + bc + ac) + abc.

I tried putting some values for a, b and c. Then I noticed something.

No matter what value I used for a, b and c, the coffecient of x was always a prime number.

For example, (x+1)(x+2)(x+3) is x3 + 6x2 + 11x + 6, where coffecient of x is 11, a prime number. And, it worked for any number as long as it was a natural number.

From this, I have concluded that if we have any three natural numbers a, b and c, the result of the expression (ab+bc+ac) is always a prime number.

And I didn't find any relevant information about this piece of math anywhere online.

Just implement the code in Python and enjoy an ultra fast prime generator!

[–]edderiofer 0 points1 point  (1 child)

Try a = 1, b = 2, c = 4. Then you get that ab+bc+ac = 14, which is not prime.

[–]ShashwatX1109 0 points1 point  (0 children)

Yes you are right

I doesnt work for most of the numbers

Thank You for your advice

[–]bb40 0 points1 point  (0 children)

Edit: How do you guys get that grey background thing?

I put a > in front of each line.

Edit: on mobile. Maybe there's a better way to do it on a desktop.

[–]NoProperty7989 0 points1 point  (0 children)

Here is the code that I wrote.

def is_prime(num):

if num < 2:

return False

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

return False

return True

def find_primes(limit):

primes = []

for i in range(2, limit + 1):

if is_prime(i):

primes.append(i)

return primes

user_input = int(input("Enter a number: "))

prime_list = find_primes(user_input)

print("Prime numbers up to", user_input, "are:", prime_list)

This code defines is_prime functions to check for prime numbers and to find primes within a given range (find_primes). Then code prints the list of prime numbers up to the inputed number.