Java Display the Prime Factorization of a number

前端 未结 7 1527
闹比i
闹比i 2020-11-30 13:02

So for my assignment, I have to write a program that asks the user for an integer input and then print out that number\'s prime factorization. This is what I have:



        
相关标签:
7条回答
  • 2020-11-30 13:35

    you can also get some help from the below function.

    public int getPrimeNumber(double number) {
        int j = 0;
        while (number % 2 == 0) {
            number = number / 2;
            j = 2;
        }
    
        for (int i = 3; i <= number; i = i + 2) {
            while (number % i == 0) {
                number = number / i;
                j = i;
            }
        }
    
        return j == 0 ? 1 : j;
    }
    

    This function will return the largest prime factor of the given number.

    0 讨论(0)
提交回复
热议问题