Prime Numbers in Java - Algorithms

前端 未结 6 890
南笙
南笙 2021-01-22 15:48

I have started learning to code in Java and decided I would use the Project Euler site to give me little tasks to try and complete with each bit of new coding I learn. So I came

6条回答
  •  粉色の甜心
    2021-01-22 16:21

    Thank you for all your help, after reading through the comments and answers I managed to condense the code much further to the following:

        public class Largest_Prime_Factor_NEW_SOLUTION_2 {
    
        static long Tn = 600851475143L;
    
        public static void main(String[] args) {
    
            for (long i = 2; i < Math.sqrt(Tn); i++) {
    
                if(Tn % i == 0) {
                    Tn = Tn / i;
                    i--;
                }   
            }
            System.out.println(Tn);
        }
    }
    

    and it works perfect! Thanks again for your help and time to help me understand. I understand it was more a mathematical problem than a coding problem, but it helped me understand a few things. I'm now off to learn something else :)

提交回复
热议问题