Prime Factorization Program in Java

后端 未结 12 1746
自闭症患者
自闭症患者 2021-01-03 11:11

I am working on a prime factorization program implemented in Java. The goal is to find the largest prime factor of 600851475143 (Project Euler problem 3). I think I have m

12条回答
  •  星月不相逢
    2021-01-03 12:00

    Here is very elegant answer - which uses brute force (not some fancy algorithm) but in a smart way - by lowering the limit as we find primes and devide composite by those primes...

    It also prints only the primes - and just the primes, and if one prime is more then once in the product - it will print it as many times as that prime is in the product.

        public class Factorization {
        public static void main(String[] args) {
        long composite = 600851475143L;
        int limit = (int)Math.sqrt(composite)+1;
        for (int i=3; i

提交回复
热议问题