Finding Largest Prime Number Out of 600851475143

How to find largest prime factor of 600851475143 in java

How about this solution:

public static long factor(long rc) {

long n = rc;

List<Long> pfactors = new ArrayList<Long>();

for (long i = 2 ; i <= n ; i++) {

while (n % i == 0) {
pfactors.add(i);

n = n / i;

}

}

return pfactors.get(pfactors.size() - 1);
}

Runs fast for me.

Find largest prime factor for the number 600851475143L

try this solution :

public class LargestPrimeFactor 
{

public static int largestPrimeFactor(long number) {
int i;

for (i = 2; i <= number; i++) {
if (number % i == 0) {
number /= i;
i--;
}
}

return i;
}

public static void main(String[] args) {
System.out.println(LargestPrimeFactor.largestPrimeFactor(600851475143l));
}

}

The problem was because you have nested loop with very big number, and that what made the loop

Greatest Prime Factor

let primeFactor = x => {    if (x === 1 || x === 2) {        return x;    }
while (x % 2 === 0) { x /= 2; } if (x === 1) { return 2; }
let max = 0; for (let i = 3; i <= Math.sqrt(x); i += 2) { while (x % i === 0) { x /= i; max = Math.max(i, max); } }
if (x > 2) { max = Math.max(x, max); } return max;};
console.log(primeFactor(35));console.log(primeFactor(13195));console.log(primeFactor(27));console.log(primeFactor(1024));console.log(primeFactor(30974914));console.log(primeFactor(600851475143));


Related Topics



Leave a reply



Submit