Answers for "Prime Factors of a Number"

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
0

Find the prime factors of a number

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print(n, 'equals', x, '*', n//x)
...             break
...     else:
...         # loop fell through without finding a factor
...         print(n, 'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
Posted by: Guest on September-07-2021

Code answers related to "Prime Factors of a Number"

Browse Popular Code Answers by Language