janaindrajit / primePy

This module cal quickly find prime factors with multiplicity of a given number. This module also has several other prime factor functions.
MIT License
3 stars 4 forks source link

If every prime factor is prime number then, how 1️⃣ is a prime factor? ⚠️ ⚠️ ⚠️ #7

Open artbindu opened 1 year ago

artbindu commented 1 year ago

We know every prime factor of a number is a prime number.

By definition, a prime factor is a prime number that divides the original number exactly and without a remainder. If a number is a prime factor, it must be a prime number.


This is the definition according to your documentation:

This is my code:

from primePy import primes

n = 1
print('lowest prime factor', primes.factor(n))
print('all prime factors: ', primes.factors(n))

Output:

lowest prime factor 1
all prime factors:  [1]

We know, 1 is neither a prime nor a composite number. Prime numbers are defined as positive integers greater than 1 that are only divisible by 1 and themselves, and 1 is not greater than 1.


Please resolve this issue.

artbindu commented 1 year ago

I just saw one issue there which is resolved: https://github.com/janaindrajit/primePy/issues/5

But still it's reproducing

artbindu commented 12 months ago

Completely freezing compilation when we pass 0 or Negative Value by mistack.

It should return None or some valid message.

artbindu commented 12 months ago

Hi @janaindrajit Can you allow me to give access to create a PR for this issue ? I already have solution for it.

#Calculates the lowest prime factor by default
def factor(num:int)->int:
    '''

    Parameters
    ----------
    num : int
        Input number

    Returns
    -------
    int
       Lowest prime factor

    '''
    if num == 1 or num <= 0:
        return None
    if num==2 or num%2==0:
        return 2
    if num==3 or num%3==0:  # dakra added
        return 3            # dakra added
    else:
        for i in range(6, int(sqrt(num))+7, 6): # dakra Primes > 3 are all either
              if num%(i-1)==0: return i-1       # dakra 5 mod 6 or
              if num%(i+1)==0: return i+1       # dakra 1 mod 6
                                                # dakra saving all the tests for number which are 3 mod 6.
        else:
            return num

def factors(num:int)->list:
    '''

    Parameters
    ----------
    num : int
        Given number

    Returns
    -------
    list
        List of prime factors

    '''
    fact=factor(num)
    if(fact) :
        new_num=num//fact
        factors=[fact]
        while new_num!=1:
            fact=factor(new_num)
            factors.append(fact)
            new_num//=fact
        return factors
    else :
        return []
artbindu commented 10 months ago

Issue is fixed in this branch https://github.com/artbindu/primePybugFixed

Commit: https://github.com/artbindu/primePybugFixed/commit/fe9aa90a0d2075ac8827838385be20f121834422

Please verify once @janaindrajit