簡體   English   中英

使用Java的素數檢查器

[英]Prime number checker using Java

我只是Java的初學者。 我已經嘗試使用我已經知道的方法來構建素數檢查器。 我使用一種方法來找到給定數字的平方根,然后將數字除以小於該根的所有整數,如果在任何情況下答案為“ 0”,則該數字不是質數; 否則,是的。


我的程序在整數數據類型(最大編號為2147483647)下運行良好,但是在每個數字之后都給出相同的輸出,例如->“是!數字是質數”。 因此,我嘗試使用double數據類型,但結果仍然相同! 對於2147483647之后的每個數字,它表示是質數。


問題 :使用Math.floor()double存儲更大的舍入數后,當我打印ArrayList它在其中顯示元素“ 0”,但最終結果條件if (contains(0) == true )為繞過並且為大於2147483647的數字實現if ( contains (0) == false )


使用Integer數據類型的第一個代碼:

import java.util.ArrayList;
import java.util.Scanner;
public class UltimatePrime {
    public static void main (String[] args)
    {
        int mod;
        Scanner input = new Scanner(System.in);
        int number = (int) input.nextDouble(); //separate and get only integer part from the input 

        if (number >= 2 && number < 2147483647) //2147483647 is the limit for data type Integer
        {
            int j = (int) Math.sqrt(number); //get the integer square root of the input and assign it to j
            ArrayList modStorage = new ArrayList();
            for (; j>1; j--)
            {
                mod = number % j;  //divide the number with all numbers less than or equal to j
                modStorage.add(mod); //store all modulus or remainder operator results in ArrayList modStorage
            }
                if (modStorage.contains(0) == true) //if ArrayList modStorage contains 0 remainder than result = true
                {
                    System.out.println("Sorry" + ", " + number + " " + "is not a prime number.");
                }
                if (modStorage.contains(0) == false) //if ArrayList modStorage doesn't contain 0 remainder than result = false
                {
                    System.out.println("Yes!!" + " " + number + " " + "is a prime number.");
                }
            }

        else if ( number == 1) //special case for number 1
        {
            System.out.println("A prime number has only two factors: 1 and itself."
                    + "\nA composite number has more than two factors."
                    + "\nThe number 1 is neither prime nor composite.");
        }

        else //insuarace :D
        {
            System.out.println("Please enter proper number!");
        }

        input.close();
    }
}

第二個代碼使用double

import java.util.ArrayList;
import java.util.Scanner;
public class FinalPrime {
    public static void main (String[] args)
    {
        double mod;
        Scanner input = new Scanner(System.in);
        double number = input.nextDouble(); //separate and get only integer part from the input 
        number = Math.floor(number);
        if (number >= 2) 
            {
                double j = Math.sqrt(number); //get the integer square root of the input and assign it to j
                j = Math.floor(j);
                ArrayList modStorage = new ArrayList();
                for (; j>1; j--)
                {
                    mod = number % j;  //divide the number with all numbers less than or equal to j
                    modStorage.add(mod); //store all modulus or remainder operator results in ArrayList modStorage
                }
                if (modStorage.contains(0) == true) //if ArrayList modStorage contains 0 remainder than result = true
                {
                    System.out.printf("%.0f \n",number);
                    System.out.println("Sorry" + ", " + "it is not a prime number.");
                }
                if (modStorage.contains(0) == false) //if ArrayList modStorage doesn't contain 0 remainder than result = false
                {
                    System.out.printf("%.0f \n",number);
                    System.out.println("Yes!!" + ", " + "it is a prime number.");
                }
            }

        else if ( number == 1) //special case for number 1
            {
                System.out.println("A prime number has only two factors: 1 and itself."
                                + "\nA composite number has more than two factors."
                                + "\nThe number 1 is neither prime nor composite.");
            }

        else //insuarace :D
            {
                System.out.println("Please enter proper number!");
            }

        input.close();
    }
}

您的問題是int overflow 2147483647int的最大值。 此數據類型不能存儲更大的數字。

Double應該用於浮點數。

要使用大整數,請使用BigInteger類,它是java.math類。 只要您的計算機上有足夠的內存,此類就可以存儲無限大的數字。

編輯:

當您有興趣了解BigInteger工作原理時,我決定編輯答案,並向您介紹BigInteger的世界。 首先讓我保證:您了解嗎,不是為大整數鍵入double嗎? 萬一足夠,您可以使用long ,它是大於int整數的類型。

如果long不夠long ,則應嘗試使用BigInteger 對於巨大的floatBigDecimal類。 讓我們專注於BigInteger

的BigInteger

BigInteger類有三個public static字段BigInteger S: ONETENZERO 如果要檢查BigInteger等於0(是的, BigInteger也可以包含小整數),則將其與BigInteger.ZERO進行比較肯定比創建新對象BigInteger(0)更好。

施工

構造函數呢? 最常用的有三種。 第一個使用String作為參數,第二個使用String和一個int基數(數字系統的基數),第三個使用位數組。

我用來構造BigIntegers的最后一種方法是一個稱為valueOf()public static方法,該方法需要longint作為參數。

BigInteger a = new BigInteger("1024"); // creates BigInteger representing number 1024.
BigInteger b = new BigInteger(1024); //also creates BigInteger representing number 1024.
BigInteger c = new BigInteger("10000000000", 2); //also creates BigInteger representing 1024

操作

要檢查BigInteger a等於BigInteger b只需鍵入

if (a.equals(b)) {...}

要添加兩個BigIntegera,b類型

 BigInteger c = a.add(b);
 //or
 a = a.add(b);

隨意閱讀文檔 ,很棒! 無論如何,如果您有任何問題,請隨時在評論部分提出。 我會回復到周日晚上。

暫無
暫無

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

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