Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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]
        
Comment

How could you find all prime factors of a number?

public static List<int> PrimeFactors(int n)
{
    if (n == 1)
    {
        return null;
    }
    var list = new List<int>();
    if (n % 2 == 0)
    {
        list.Add(2);

        while (n % 2 == 0)
        {
            n /= 2;
        }
    }

    for (int i = 3; i <= Math.Sqrt(n); i += 2)
    {
        if (n % i == 0)
        {
            list.Add(i);

            while (n % i == 0)
            {
                n /= i;
            }
        }
    }

    if (n > 2)
        list.Add(n);
    return list;
}
Comment

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;
}
Comment

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
Comment

prime factors of a number

public static List<int> PrimeFactors(int n)
{
    if (n == 1)
    {
        return null;
    }
    var list = new List<int>();
    if (n % 2 == 0)
    {
        list.Add(2);

        while (n % 2 == 0)
        {
            n /= 2;
        }
    }

    for (int i = 3; i <= Math.Sqrt(n); i += 2)
    {
        if (n % i == 0)
        {
            list.Add(i);

            while (n % i == 0)
            {
                n /= i;
            }
        }
    }

    if (n > 2)
        list.Add(n);
    return list;
}
Comment

PREVIOUS NEXT
Code Example
Java :: what is deserialization in java 
Java :: abstract class java 
Java :: java check if array element is null 
Java :: java hashmap set value 
Java :: Load array of strings from console 
Java :: deep content 
Java :: output folder director 
Java :: fragment to activity typecasting 
Java :: BufferedReader x = new BufferedReader(new InputStreamReader(System.in)); 
Java :: Multi basic auth with spring security 
Java :: java cant use string functions after changing an integer into string 
Java :: java import keyword 
Java :: jbutton actionperformed 
Java :: javax.net.ssl.trustStore trustall 
Java :: coin flip random java 
Java :: Transversal operation in java array 
Java :: functionality of consumer functional interface in java 
Java :: Exercise. Create a simple Java program using array named SumOfArray.java that will accept an input of whole numbers or floating point numbers and will return the Sum result. The program must accept at least 5 numbers. 
Java :: Picked up _JAVA_OPTIONS: -Xmx256M intillij 
Java :: calculate tip and sales tax function 
Java :: Implementation of HashMap Class in Java map 
Java :: java nom de la methode actuel 
Java :: gradle project load test data json file with jackson 
Java :: code for working clock in java eclipse 
Java :: a to double 
Java :: getBatteryPercentage android studio 
Java :: sort array from certain index java 
Java :: java lambda expressions qunado foi implantada 
Java :: transpose array in java 
Java :: Java program to demonstrate working of HashTable 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =