Find all prime factors (Java)

CSDN Topic Challenge Phase 2
Participation Topic: Algorithm Solution

1. Description of the topic

A composite number can be expressed as the multiplication of several prime numbers, such as 21=3×7, 18=2×3×3, and these prime numbers are called its prime factors.
Given a composite number n(n≤2^31-1), find all its prime factors.

Format

Input format
The input is only one line, which is a positive integer n

Output format
Output one line, all the prime factors of n, separated by spaces, the prime factors must be arranged in ascending order

sample
input sample
30
output sample
2 3 5

2. Topic link

172.22.114.196

3. Idea explanation

Loop from i = 2, if the remainder is 0 when n is divided by i, continue to divide by i until n % i is not equal to 0. Then i++, until i = n ends.

In addition, every time i++ does not need to judge whether i is a prime number, only prime numbers can be divisible.

4. Code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = n,flag = 0;

        for(int i = 2;i <= m;i++) {
            if(n % i == 0) {
                while (n % i == 0) {
                    if(flag == 0) {
                        System.out.print(i);
                        flag = 1;
                    }
                    else {
                        System.out.print(" " + i);
                    }
                    n /= i;
                }
            }
        }

        scanner.close();
    }
}

5. Impressions

This question is a very simple algorithm question, not too difficult.

But I have been entangled in judging whether i is a prime number before, using two different methods. One timeout, one out of memory.

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = n,flag = 0;

        for(int i = 2;i <= m;i++) {
            int isPrime = 1;
            for(int j = 2;j*j <= i;j++) {
                if(i % j == 0) {
                    isPrime = 0;
                    break;
                }
            }
            if(isPrime == 1 && n % i == 0) {
                //System.out.println("i = " + i + " n = "+ n);
                while (n % i == 0) {
                    if(flag == 0) {
                        System.out.print(i);
                        flag = 1;
                    }
                    else {
                        System.out.print(" " + i);
                    }
                    n /= i;
                }
            }
        }

        scanner.close();
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = n,flag = 0;
        int[] isPrime = new int[n+5];//prime-0


        for(int i = 2;i*i <= n;i++) {
            if(isPrime[i] == 0) {
                for(int j = 2;j*i < n+5;j++) {
                    isPrime[i*j] = 1;
                }
            }
        }
        isPrime[1] = 1;
        isPrime[2] = 0;

        for(int i = 2;i <= m;i++) {
            if(isPrime[i] == 0 && n % i == 0) {
                //System.out.println("i = " + i + " n = "+ n);
                while (n % i == 0) {
                    if(flag == 0) {
                        System.out.print(i);
                        flag = 1;
                    }
                    else {
                        System.out.print(" " + i);
                    }
                    n /= i;
                }
            }
        }

        scanner.close();
    }
}

Here is some more test data:

100
2 2 5 5

5000
2 2 2 5 5 5 5

345678
2 3 17 3389

Guess you like

Origin blog.csdn.net/m0_61465701/article/details/126897994