Ask coding questions

← Back to all posts
How to work dis
advaydhiraj (5)

IM trying to make a project that prints out the first 25 prime numbers but it doesn't work

Answered by MoltasDev (10) [earned 5 cycles]
View Answer
Commentshotnewtop
MoltasDev (10)

You need to run multiple iterations of the prime number check stuff in your function; You need to make a loop where it runes the number of times you want. Here I have made a loop using your last if statement as an exit condition. https://repl.it/@MoltasDev/Something-Cool

advaydhiraj (5)

@MoltasDev This only runs it 25 times and doesn't print out exactly 25 primes

MoltasDev (10)

@advaydhiraj Oh, my bad. Read the question wrong. Then just do a loop checking if the length of the prime number array is 25.

advaydhiraj (5)

@MoltasDev Yeah, I figured out what you were trying to do so know if you check the mini iframe up top it should work.

Roar123 (427)

What is your technique for calculating primes? I wrote a quick program in Python3 to calculate this - you can use a similar technique.

# Python3
def prime_check(n):
    # Creates list with factors and measures length
    factor_len = len([i for i in range(1,n+1) if n % i == 0])
    # Number with 2 factors is prime
    if factor_len == 2:
        return "Prime"
    else:
        pass

prime_list = []
number = 2

while len(prime_list) < 25:
    prime = prime_check(number)
    if prime == "Prime":
        # Adding number to list if prime
        prime_list.append(number)
    else:
        pass
    number += 1

print(prime_list)
Roar123 (427)

@advaydhiraj You're welcome! Were you able to understand the Python example? And also if I answered your question, you should mark my response as the answer.

advaydhiraj (5)

@Roar123 Sorry im new so i dont know how to work dis.

ChezCoder (1467)

um u didn't call the function draw. you only declared it

ChezCoder (1467)

just write draw() at the end of the code