-5

Not sure why trying to find prime factors like this doesn't work:

primes= [2,3,5]
maximum = int(input("Highest number checked: "))
x = 2
prime = False
while x < maximum:
    y = len(primes)
    for n in range(2,y):
        if x % int(primes[n]) == 0:
            prime = True
        else:
            prime = False
            break
    if prime == True:
        primes.append(x)
    x = x+1

print(primes)

Inputting 30 prints 2,3,5,5,10,20

Lily
  • 11
  • 3
  • 1
    Would you like to share the error message such that we can look into the problem or are you just using this site as your personal journal? – timgeb Dec 10 '17 at 16:11
  • I just assumed someone would be able to spot my error, sorry? – Lily Dec 10 '17 at 16:13
  • Well, someone (probably) already did, but it would be way easier if you gave us the complete error message. 'bool error' says nothing. – timgeb Dec 10 '17 at 16:14
  • The issue is not resolved and I edited it pal – Lily Dec 10 '17 at 16:20
  • When I read your code I find I'm unsure of what your program is *supposed* to do. You seem to have predefined a tiny list of primes. Are those the only ones you are going to check for? That means you can only factor very small numbers. – President James K. Polk Dec 10 '17 at 19:04

2 Answers2

0

In this line if x % int(prime[n]) == 0: change prime to primes.

Ahmad
  • 906
  • 11
  • 27
  • even with this change the program doesn't work, I inputted 30 and got back 2,3,5,5,10,20 – Lily Dec 10 '17 at 16:16
  • prime i bool variable and not list. but you write prime[n]. it is not correct – Ahmad Dec 10 '17 at 16:24
  • what is your problem? please explain complete – Ahmad Dec 10 '17 at 16:25
  • When I execute the program, if I input the maximum as 30, it should give me back a list of primes but it gives me back 2,3,5,5,10,20 which is of course wrong so I must have logic error – Lily Dec 10 '17 at 16:28
  • it's meant to give prime factors so of 30 would be 2,3,5 – Lily Dec 10 '17 at 16:31
  • please check this page for find your answer: https://stackoverflow.com/questions/16996217/prime-factorization-list – Ahmad Dec 10 '17 at 16:34
0

Not sure why trying to find prime factors like this doesn't work

It doesn't work because you didn't write a (broken) prime factor finding program, you wrote a (broken) prime finding program. If we fix it and clean it up, this should be a bit more obvious:

primes = [2, 3, 5]

maximum = int(input("Highest number checked: "))

x = 2

while x <= maximum:
    is_prime = True

    for prime in primes:
        if prime * prime > x:
            break

        if x % prime == 0:
            is_prime = False
            break

    if is_prime:
        primes.append(x)

    x = x + 1

print(primes)

USAGE

% python3 test.py
Highest number checked: 100
[2, 3, 5, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
%

This can be used as part of a prime factor finding program, but isn't sufficient on its own. You need to work through some subset of the list of primes up to N seeing which primes are factors of N. Not substantially more code than you have now.

cdlane
  • 40,441
  • 5
  • 32
  • 81