Search code examples
pythonpython-3.xlistprimes

Why is my simple prime number checker not working?


I have made changes to my code which helped the size but still get the error

IndexError: list index out of range

But I cannot seem to find out why. I know it has to do with [-1] but I can't seem to figure out why it isn't calling the last listed element.

number = 4
not_primes = 0
primes = []
while 1:
    for i in range(2, number):
        if number % i == 0:
            i = not_primes
        else:
            primes.append(number)
        print(primes[-1])
        number += 1

Solution

  • try this code (with little change on your code):

    current_number = 2
    not_prime = list()
    prime_numbers = list()
    
    def odd_checker():    
        if current_number % 2 == 0 and current_number != 2:
            not_prime.append(current_number)
        else:
            last_digit_check()
    
    def last_digit_check():
        if current_number % 5 == 0 and current_number != 5:
            not_prime.append(current_number)
        else:
            last_prime_checker(current_number)
    
    def last_prime_checker(the_number):
        for i in prime_numbers: 
            if the_number % i == 0:
                not_prime.append(the_number)
                return
        prime_numbers.append(the_number)
    
    while True:
        odd_checker()
        current_number += 1
        print(prime_numbers)
        
        y_n = input('do you continue(y/n) : ') if current_number % 10 == 0 else ''
        if y_n == 'n':
            break
    

    output:

    [2]
    [2, 3]
    [2, 3]
    [2, 3, 5]
    [2, 3, 5]
    [2, 3, 5, 7]
    [2, 3, 5, 7]
    [2, 3, 5, 7]
    do you continue(y/n) : y
    [2, 3, 5, 7]
    [2, 3, 5, 7, 11]
    [2, 3, 5, 7, 11]
    [2, 3, 5, 7, 11, 13]
    [2, 3, 5, 7, 11, 13]
    [2, 3, 5, 7, 11, 13]
    [2, 3, 5, 7, 11, 13]
    [2, 3, 5, 7, 11, 13, 17]
    [2, 3, 5, 7, 11, 13, 17]
    [2, 3, 5, 7, 11, 13, 17, 19]
    do you continue(y/n) : y
    [2, 3, 5, 7, 11, 13, 17, 19]
    [2, 3, 5, 7, 11, 13, 17, 19]
    [2, 3, 5, 7, 11, 13, 17, 19]
    [2, 3, 5, 7, 11, 13, 17, 19, 23]
    [2, 3, 5, 7, 11, 13, 17, 19, 23]
    [2, 3, 5, 7, 11, 13, 17, 19, 23]
    [2, 3, 5, 7, 11, 13, 17, 19, 23]
    [2, 3, 5, 7, 11, 13, 17, 19, 23]
    [2, 3, 5, 7, 11, 13, 17, 19, 23]
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    do you continue(y/n) : n
    

    EDIT : Adding new code for finding prime the number as request on comment.

    number = 4
    not_primes = False
    primes = []
    while 1:
        
        for i in range(2, number):
            if number % i == 0:
                not_prime = True
        
        if not_prime == False:
            primes.append(number)
            
            
        try:
            print(primes[-1])
        except:
            print('do not find prime yet')
            
            
        number += 1
        not_prime = False
        
        y_n = input('do you continue(y/n) : ') if number % 10 == 0 else ''
        if y_n == 'n':
            break
    

    output:

    do not find prime yet
    5
    5
    7
    7
    7
    do you continue(y/n) : y
    7
    11
    11
    13
    13
    13
    13
    17
    17
    19
    do you continue(y/n) : y
    19
    19
    19
    23
    23
    23
    23
    23
    23
    29
    do you continue(y/n) : n