Python Forum
Prime number Python homework - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Prime number Python homework (/thread-17948.html)

Pages: 1 2


Prime number Python homework - mkrisz98 - Apr-30-2019

Hi, I got a homework and I have to ask for some help.
My task is:
Listing the first six prime numbers, we get 2, 3, 5, 7, 11, and 13, where 13 is prime number 6. Write a program that defines the prime number N. Create an effective implementation that can calculate 10,000 prime numbers in real time.

Please somebody help me.
Thanks!


RE: Prime number Python homework - Larz60+ - Apr-30-2019

Please show what you have tried so far.
We're here to help, but not do the work for you.


RE: Prime number Python homework - mkrisz98 - May-02-2019

My recent program can only list the prime numbers in an interval.

number = int(input("How many prime numbers do you want ? "))

cnt = 0
for i in range (1, number+1):
    for j in range(1, i+1):
        if(i%j == 0):
            cnt += 1
    if(cnt == 2):
        print(i)
    cnt = 0



RE: Prime number Python homework - DeaD_EyE - May-02-2019

Write a function, which checks a number, if it's a prime.
In another function you collect primes, until you have the number of primes you want to have.

from itertools import count


def is_prime(number):
    """Your implementation"""
    return True


def get_primes(n):
    primes = []
    while len(primes) < n:
        for number in count():
            if is_prime(number):
                primes.append(number)
    return primes
The function to check the prime can also taken from other libraries.
If you make the easiest implementation, it takes more time to check primes.


RE: Prime number Python homework - mkrisz98 - May-09-2019

I wrote this code, but i don't know why is it exit with exit code 0.


from itertools import count


def is_prime(number):
    if number <= 1:
        return False
    for i in range(2, number):
        if number % i == 0:
            return False;

    return True



def get_primes(n):
    primes = []
    while len(primes) < n:
        for number in count():
            if is_prime(number):
                primes.append(number)
    return primes



RE: Prime number Python homework - mkrisz98 - May-10-2019

Okay, I realized I didn't printed it last time, but if I print it like this, it is just running and running and never stops.
Can you help me what did I wrong?


from itertools import count


def is_prime(number):
    if number <= 1:
        return False
    for i in range(2, number):
        if number % i == 0:
            return False;

    return True



def get_primes(n):
    primes = []
    while len(primes) < n:
        for number in count():
            if is_prime(number):
                primes.append(number)
    return primes
print(get_primes(1))



RE: Prime number Python homework - Yoriz - May-10-2019

from itertools import count will keep returning values forever.
for number in count(): has no way of ending
while len(primes) < n: only happens once

The following code Removes the while loop and gives the for loop a way out.
from itertools import count
 
 
def is_prime(number):
    if number <= 1:
        return False
    for i in range(2, number):
        if number % i == 0:
            return False;
 
    return True
 

 
def get_primes(n):
    primes = []
    for number in count():
        if is_prime(number):
            primes.append(number)
        if len(primes) == n:
            return primes


print(get_primes(1))
Output:
[2]



RE: Prime number Python homework - mkrisz98 - May-10-2019

Thank you so much, this is exactly what i was whinking for!


RE: Prime number Python homework - Yoriz - May-10-2019

Can be improved as it only needs to check len of found primes after appending a new prime, no need to check otherwise.
from itertools import count
 
 
def is_prime(number):
    if number <= 1:
        return False
    for i in range(2, number):
        if number % i == 0:
            return False;
 
    return True
 
 
def get_primes(n):
    primes = []
    for number in count():
        if is_prime(number):
            primes.append(number)
            if len(primes) == n:
                return primes


print(get_primes(6))
Output:
[2, 3, 5, 7, 11, 13]


More efficient a second count to keep count Dance
from itertools import count
 
 
def is_prime(number):
    if number <= 1:
        return False
    for i in range(2, number):
        if number % i == 0:
            return False;
 
    return True
 
 
def get_primes(n):
    primes = []
    qty_found = count(1)
    for number in count():
        if is_prime(number):
            primes.append(number)
            if next(qty_found) == n:
                return primes


print(get_primes(6))
Output:
[2, 3, 5, 7, 11, 13]



RE: Prime number Python homework - mkrisz98 - May-10-2019

Yes it is better. My task is to list 10,000 prime numbers in real time, this code can list it in 30 seconds,
but i think it's real time and enough for me.