「java输出素数因子」编写程序输出用户指定数据的所有素数因子
今天给各位分享java输出素数因子的知识,其中也会对编写程序输出用户指定数据的所有素数因子进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java输入一个整数,输出其所有素数因子
import java.util.ArrayList;
import java.util.List;
import java.lang.Math;
public class Solution {
private int num;
private ListInteger primeList;
private int index;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Solution(int num) {
this.num = num;
primeList = new ArrayListInteger();
primeList.add(3);
}
public ListInteger findPrimeContainedList(){
index=0;
int operationNum = num;
ListInteger list = new ArrayListInteger();
int stop = (int) Math.floor(Math.sqrt(operationNum));
if(stop = 2){
return list;
}
operationNum = checkAndRemovePrimeNumber(operationNum,2,list);
stop = (int) Math.floor(Math.sqrt(operationNum));
int p = getNextPrimeNumber();
while(p = stop){
operationNum = checkAndRemovePrimeNumber(operationNum,p,list);
stop = (int) Math.floor(Math.sqrt(operationNum));
p = getNextPrimeNumber();
}
list.add(operationNum);
return list;
}
private int checkAndRemovePrimeNumber(int num,int p,ListInteger resultList){
boolean f = false;
while(num%p == 0){
num/=p;
f = true;
}
if(f){
resultList.add(p);
}
return num;
}
private int getNextPrimeNumber() {
if(this.primeList.size()index++){
return this.primeList.get(index-1);
}
int next = this.primeList.get(this.primeList.size()-1) + 2;
while(true){
int stop = (int) Math.floor(Math.sqrt(next));
boolean f = true;
for(int n: this.primeList){
if(n stop){
break;
}
if(next%n==0){
f = false;
break;
}
}
if(f){
this.primeList.add(next);
break;
}else{
next += 2;
}
}
return next;
}
public static void main(String[] args) {
Solution s = new Solution(123);
System.out.println(s.findPrimeContainedList());
s.setNum(240);
System.out.println(s.findPrimeContainedList());
}
}
好久没写JAVA了。逻辑很简单,做了点优化。
JAVA程序求助!编写程序输出用户指定数据的所有素数因子
你看看 可以求出所有素数因子 比如数字是15 那么他的素数因子求的是:
3,5,1 如果本身是素数 那么求的就是 他本身和1
import java.util.Scanner;
public class shushu
{
/**
*
*/
public shushu()
{
// TODO Auto-generated constructor stub
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();
/* System.out.println(n + "的本身因子是:" + n); //数的本身也是他的因子
*/
int left = n;
int j = left;
System.out.print(n + "的素数因子是:");
while (j=1)
{
int temp = j--;
if(left % temp == 0 isSushu(temp))
{
System.out.print(temp) ;
if(left == 1)
break;
System.out.print(",");
left /= temp;
j = left;
}
}
}
/**
* TODO 判断某数是否素数
* @param n
* @return
* return: boolean
* author: zenglulin
* time: 下午02:13:40
*/
public static boolean isSushu(int n)
{
for (int j = 2; j n; j++)
{
if(n %j == 0)
return false;
}
return true;
}
}
Java语言输出一个整数所有素数因子
这主要是考察对素数判定吧。
class T {
public static boolean isPrime(int n) {
if (n2) return false;
if (n==2) return true;
for (int i=3;i=Math.sqrt(n);i++) {
if (n%i==0) return false;
}
return true;
}
public static void main(String argv[]) {
int m = 123456789;
for (int i=1;i=m;i++) {
if (m%i==0 isPrime(i))
System.out.print(i+",");
}
}
}
关于java输出素数因子和编写程序输出用户指定数据的所有素数因子的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-07,除非注明,否则均为
原创文章,转载请注明出处。