0

I'm trying to find a prime factor of the number 705561475143.

In my code, the function primeFactor() searches for all the factors in a certain integer, plugs them into the function Prime(), which either changes the number into 0 or doesn't change it at all. If the number isn't changed to 0, it's a prime number that should be added to the list.

import math


def Prime(n):
    isPrime = False
    r = range(2, int(math.sqrt(n)+5))
    a = 1
    for b in r:
        if r[a] >= int(math.sqrt(n)+2):
            break
        elif n % r[a] != 0:
            a += 1
        elif n % r[a] == 0 & n != r[a] & n != 1:
            n = 0
            break

def primeFactor(m):
    l = []
    for x in range(1, m+1):
        if m % x == 0:
            if Prime(m) != 0:
                l.append(x)
    print(l)

However, when I input

> primeFactor(36)

I get

> [1,2,3,4,6,9,12,18,36]

Instead of

> [2,3]

And when I input

> primeFactor(705561475143)

The code cannot give me an answer.

1 Answer 1

4

Firstly, why do you use m+1 for the x in range()? Note, you might want to only use range(2, m/2, 1) - see if you can figure out why.

Secondly,

    if m % x == 0:
        if Prime(m) != 0:
            l.append(x)

If m is divisible by x then it can't be prime and calling Prime(m) is useless. I suspect you meant to pass x as the argument. (Assuming x is not 0 or == m)

Thirdly Prime() never returns anything. This means that it is always != 0.

Finally, to help you out a bit, I'll link you to some of my own answers to this question.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I tried running this, but it didn't recognize some functions (ie primeList). What imports did you additionally use?
@LaPXL8R - I purposefully did not include primeList(). I did however end up spending an hour or so tweaking my code to be more optimised, I will update this post now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.