Answers for "generate prime factors of a given integer"

1

python prime factors

# There is no quick way to calculate the prime factors of a number.
# In fact, prime factorization is so famously hard that it's what puts the "asymmetric" in asymmetric RSA encryption.
# That being said, it can be sped up a little bit by using divisibility rules, like checking if the sum of the digits is divisible by 3.

def factors(num):
        ps = [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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149] # Primes from https://primes.utm.edu/lists/small/10000.txt. Primes can also be generated by iterating through numbers and checking for factors, or by using a probabilistic test like Rabin-Miller.
        pdict = {}
        for p in ps:
                if p <= num:
                        while (num / p).is_integer():
                                if str(p) in pdict:
                                        pdict[str(p)] += 1
                                else:
                                        pdict[str(p)] = 1
                                num /= p
                if num == 1: break
        return pdict

# Returns a dictionary in the form {"base": "exponent"}
Posted by: Guest on July-05-2020
1

How could you find all prime factors of a number?

function primeFactors(n){
  var factors = [], 
      divisor = 2;
  
  while(n>2){
    if(n % divisor == 0){
       factors.push(divisor); 
       n= n/ divisor;
    }
    else{
      divisor++;
    }     
  }
  return factors;
}

> primeFactors(69);
  = [3, 23]
Posted by: Guest on May-21-2021
0

Prime Factors of a Number

def primeFactors(n):
  global factors
  if(n==1):
    return
  elif((n%factors)== 0):
    print(factors)
    primeFactors(n//factors)
  else:
    factors+=1
    primeFactors(n)
factors = 2
Posted by: Guest on August-17-2021
0

Prime factors of a number

public List<Integer> factorsOf(int n) {
  ArrayList<Integer> factors = new ArrayList<>();

  for (int d = 2; n > 1; d++)
    for (; n % d == 0; n /= d)
      factors.add(d);

  return factors;
}
Posted by: Guest on May-13-2021

Code answers related to "generate prime factors of a given integer"

Python Answers by Framework

Browse Popular Code Answers by Language