簡體   English   中英

如何分解數字並確定其是否為質數

[英]How to factor a number and determine whether its a prime number

所以我有一個問題,當我將一個數字分解為15時,我必須顯示:15 = 3x5,但是我得到的卻是3x5x5,我不知道如何制作它,所以它只顯示3x5。 然后我遇到的另一個問題是查找我輸入的數字是否是質數。 有什么解決辦法嗎? 我只需要那個,然后我會編輯其他東西。

public class PrimeFactor 
{
    public static void main(String[] args) 
    {
        Scanner input= new Scanner(System.in);
        int a;
        int d;
        int remainder=0;
        int count=2;
        int c=0;
        String s;
        System.out.println("Enter an integer to be factored:");
        a=input.nextInt();
        s="";
        d=a;
        while(a>1)
        {
            if(a>1)
            { 
                s="";
                while(a>1)
                {
                    remainder=a%count;
                    if (!(remainder>0))
                        while(remainder==0)
                        {
                            remainder=a%count;
                            if (remainder==0)
                            {    
                                a=a/count;
                                c=c+1;
                                s=s+count+"x";
                                if (a==1)
                                    s=s+count;
                            }
                            else
                                count++;
                        }
                    else 
                        count++;
                }
                if (a%count==0)
                {
                    System.out.println(d +"=" + s);
                    System.out.println(d+" is a prime number.");
                }
                else
                    System.out.println(d +"=" + s);
            }
        // TODO code application logic here
        }
    }
}    

這些是非常簡單的方法,可用於分解數字並確定其是否為質數:

public static int oneFactor(int i) {
    for (int j = 2; j < i; j++) {
        if (i % j == 0)
            return j;
    }
    return -1;
}

public static Integer[] primeFactors(int i) {
    List<Integer> factors = new ArrayList<Integer>();

    boolean cont = true;
    while (cont) {
        int f = oneFactor(i);
        if (i > 1 && f != -1) {
            i /= f;
            factors.add(f);
        } else
            factors.add(i);
        if (f == -1)
            cont = false;
    }

    return factors.toArray(new Integer[factors.size()]);
}

public static boolean isPrime(int i) {
    if (i == 2 || i == 3)
        return true;

    if (i < 2 || i % 2 == 0)
        return false;

    for (int j = 3, end = (int) Math.sqrt(i); j <= end; j += 2) {
        if (i % j == 0) {
            return false;
        }
    }
    return true;
}

我敢肯定,可以使用更快的算法,但是那是以簡單為代價的,而且看來您不需要高速方法。 它們都在整數上運行,但是很容易將它們更改為與longs一起使用。
如果你有任何問題隨時問!

這確定數字是質數還是最快的方法。 另一種方法是使用for loop來確定該數字的因數,然后如果具有兩個以上的因數,則稱其為素數。

int num; // is the number being tested for if it's prime.
boolean isPrime = true;

for (int i = 2; i <= Math.sqrt(num); i++) // only have to test until the square root of the number
{
    if (num%i == 0) // if the number is divisible by anything from 2 - the square root of the number
    {
        isPrime = false; // it is not prime
        break; // break out of the loop because it's not prime and no more testing needed
    }
}

if (isPrime) 
{
    System.out.println(num + " is a prime number.");
}
else
{
    System.out.println(num + " is a composite number.");
}

您不是很正確地構造分解字符串:

  • 當您發現3除以a = 15時,將s設置為3x並將a設置為商,因此a=5
  • 當發現5除以a = 5時,會將5x附加到s ,所以現在s3x5x 然后,設置a到商,這是1.由於該商數現在是1 ,你再追加5,所以現在你得到3x5x5

您要做的是,當a=1時僅追加5 ,而不是5x5 您必須更改此:

s=s+count+"x";
if (a==1)
    s=s+count;

對此:

if (a==1) {
    s=s+count;
} else {
    s=s+count+"x";
}

這樣嘗試如何:-

for(int i = input-1; i > 0; i--) {
    if((input % i) == 0) {
            if(i == 1)
                System.out.println("Number is a prime");
            else
                System.out.println("Number is not a prime");
            break;
    }       
 }

您要編寫一個循環,將數字1循環到(輸入的數字)。 如果找到了一個因子,則將其打印出來,然后將輸入除以該因子。 (並測試是否可以再次除以相同的數字),否則請跳至下一個數字。

繼續執行此操作,直到輸入除以1。

該程序會將數字分解為主要因素:

public class Factor {
    public static void main() {
        //Declares Variables
        int factor = 15;
        int i = 2;

        System.out.println("Listing Factors:\n");
        while (factor>1) {
            //Tests if 'i' is a factor of 'factor' 
            if (factor%i == 0) {
                //Prints out and divides factor
                System.out.println(i);
                factor = factor/i;
            } else {
                //Skips to next Number
                i++;
            }
        }
    }
}

輸出:

Listing Factors:

3
5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM