Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top