Python Forum
Prime number Python homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prime number Python homework
#1
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!
Reply
#2
Please show what you have tried so far.
We're here to help, but not do the work for you.
Reply
#3
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
Reply
#4
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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
Reply
#6
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))
Reply
#7
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]
Reply
#8
Thank you so much, this is exactly what i was whinking for!
Reply
#9
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]
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to find the next prime number FirstBornAlbratross 8 4,335 Aug-14-2023, 01:16 PM
Last Post: deanhystad
  HELP in python homework makashito 4 3,928 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 1,963 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,306 May-09-2021, 11:19 AM
Last Post: Yoriz
  calculate the 10001st prime number topologh 7 6,253 Oct-05-2020, 07:41 PM
Last Post: deanhystad
  Homework with python Johnsonmfw 1 1,690 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Prime number code ryfoa6 3 2,900 Mar-21-2020, 12:23 AM
Last Post: ryfoa6
  Trouble interpreting prime number code ryfoa6 1 2,259 Mar-20-2020, 03:47 PM
Last Post: stullis
  Python Homework Help *Urgent GS31 2 2,586 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Python Homework Question OrcDroid123 1 2,372 Sep-01-2019, 08:44 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020