Skip to content

EfficientPrimeFactorization error #2

Description

@BedirT

EfficientPrimeFactorization is wrong.

You should run your code before thinking that you are done with it:

Line 9: print([n1]) # Why do you have brackets ?
Line 10: else (n1%b)==0 or n1<b: # You cannot have condition in else statement, use elif instead

so:

Line 9:     print(n1)
Line 10: elif (n1%b)==0 or n1<b:

These were the problems with your code to give the "result", but furthermore your solution is out of concept. This algorithms aim is to find out prime factors of a number, and do this process as efficient as we can. Let me give you the brute force solution with an example:

Assume 24 as in your code, 24 = 2x2x2x3 = 2^3x3. So the result that I get for this question should be [2, 2, 2, 3], all the prime factors of 24. Now brute force solution would be (assuming I know all the prime number from 2 to 24, which you should've find using sieve):

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
factors = [] # List to keep my factors
n = 24
for i in range(len(primes)): # We will iterate through all the primes in our primes list
    while n % primes[i] == 0: # Check until n doesn't devide current prime
        n = n / primes[i] # n is devided by current prime that im checking
        factors.append(primes[i]) # put my divisor to factors list
print(factors) 

and the result will be

[2, 2, 2, 3]

Try to read this blog post of mine, if you are still confused http://bedirtapkan.com/Prime-Number-Factorization/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions