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;
}
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;
}
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;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us