0
Follow
0
View

java quality factor complements the code

cuiping3210 注册会员
2023-02-28 02:18

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > the following is a complete Java code, can be a positive integer is decomposed into the product of prime factors, and the output according to the requirement:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);//定义读取数据对象reader

        int n;

        n = reader.nextInt(); //读取一个整数

        Main pf = new Main();

        pf.printPrimeFactor(n);
    }

    void printPrimeFactor(int n) {  //求整数n的质因子,按要求输出
        System.out.print(n + "="); //输出原数n

        int i = 2; //从2开始寻找因子
        while (i <= n) {
            if (n % i == 0) { //如果n能够被i整除
                System.out.print(i); //输出i
                n /= i; //将n除以i,以便继续寻找因子
                if (n != 1) { //如果n不等于1,则继续输出乘号
                    System.out.print("*");
                }
            } else { //否则i加1
                i++;
            }
        }
    }

    int isPrimeNumber(int n) {
        if (n <= 1) { //小于等于1的数不是质数
            return 0;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) { //如果能够被整除,不是质数
                return 0;
            }
        }
        return 1; //是质数
    }
}


The program runs as follows:

Input:

90

Output:

90=2*3*3*5


qznxcyp 注册会员
2023-02-28 02:18

This answer quotes ChatGPT

Try this code

import java.util.Scanner;

public class Main {

csharp
Copy code
public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);//定义读取数据对象reader

    int n;

    n = reader.nextInt(); //读取一个整数

    Main pf = new Main();

    pf.printPrimeFactor(n);

}

void printPrimeFactor(int n) {  //求整数n的质因子,按按要求输出

    System.out.print(n + "=");

    int i = 2;

    while (i <= n) {

        if (n % i == 0) {

            System.out.print(i);

            n = n / i;

            if (n != 1) {

                System.out.print("*");

            }

        } else {

            i++;

        }

    }

}

int isPrimeNumber(int n) {

    if (n <= 1) {

        return 0;

    }

    for (int i = 2; i <= Math.sqrt(n); i++) {

        if (n % i == 0) {

            return 0;

        }

    }

    return 1;

}
}

duhuafeng2006 注册会员
2023-02-28 02:18

gpt generation bug, has been changed

cz4141 注册会员
2023-02-28 02:18
); printPrimeFactor(n); } } int isPrimeNumber ( int n) { // 判断n是否为质数,返回1为质数,返回0为非质数 int i = 2 ; while (i <= Math.sqrt(n)) { if (n % i == 0 ) { return 0 ; } i++; } return 1 ; } } .
< !- - - - - >
cyflove23 注册会员
2023-02-28 02:18

This answer is quoted from ChatGPT

public class Main { 
       public static void main(String[] args) { 
              Scanner reader=new Scanner(System.in);//定义读取数据对象reader 
              int n; 
              n=reader.nextInt(); //读取一个整数  
              Main pf=new Main(); 
              pf.printPrimeFactor(n);            
       } 
 
       void printPrimeFactor(int n){  //求整数n的质因子,按按要求输出 
              int i=2;
              System.out.print(n+"=");
              while(i<=n){
                  if(isPrimeNumber(i)==1 && n%i==0){
                      System.out.print(i);
                      n=n/i;
                      if(n!=1){
                          System.out.print("*");
                      }
                  }else{
                      i++;
                  }
              }
              System.out.println();
       } 
       int isPrimeNumber(int n){ 
              int flag=1;
              for(int i=2;iif(n%i==0){
                      flag=0;
                      break;
                  }
              }
              return flag;
       }  
}

danyuanjh 注册会员
2023-02-28 02:18
< div class = "md_content_show e397 data - v - 3967" = "" >

hello, excuse me n %! What does it mean that i(MISSING)==0?

About the Author

Question Info

Publish Time
2023-02-28 02:18
Update Time
2023-02-28 02:18

Related Question

JavaScript If else语句不符合[关闭]

Java调用Matlab运行错误直接宕机

一、为什么从IDEA打开的JavaFx scene builder,imageView没有相应呢

检查是否存在使用JavaScript获取的Youtube缩略图

关于#改成#的问题,如何解决?(语言-java|开发工具-eclipse)

javasctipt

java找不到图片怎么办

c语言怎么判断质数和output factors

如何在JavaScript中获得基于特定条件的对象值?

Java帮助-输出范围,增量为5