使用Java的素数检查器 [英] Prime number checker using Java

查看:86
本文介绍了使用Java的素数检查器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是Java的初学者。我试图使用我已经知道的建立素数检查器。我使用了一种方法,找到给定数字的平方根,然后将数字除以小于该根的所有整数,如果在任何一种情况下,答案为0,则该数字不是素数;否则,它是。

I am just a beginner in Java. I have tried to build a prime number checker using what I already know. I have used a method that finds the square root of the given number and then divides the number with all the integers less than that root and if in any one case answer is "0" then the number is not a prime; otherwise, it is.

我的程序适用于整数数据类型,最高为No.2147483647,但是在每个和它给出了相同输出的每个数字 - >是!!数字是素数。因此我尝试使用双数据类型但结果仍然相同!对于2147483647之后的每个数字,它表示它是一个素数。

My program works well with integer data type upto no.2147483647 but after this number for each and every number it gives the same output like --> "Yes!! The number is a prime number". Because of this I've tried using double data type but the result is still the same! For every number after 2147483647 it says it is a prime number.

问题:使用后当我打印 Math.floor()和 double 来存储更大的舍入数字> ArrayList 它显示元素0,但最终结果条件 if(contains(0)== true)被绕过并且 if(contains(0)== false)是针对大于2147483647的数字实施的

Question: after using Math.floor() and double to store a bigger rounded off number, when I print my ArrayList it shows element "0" in it, but the in final result condition if (contains(0) == true ) is bypassed and if ( contains (0) == false ) is implemented for numbers greater than 2147483647

使用整数数据类型的第一个代码:

First Code using the Integer data type:

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 2147483647 int 的最大值。此数据类型无法存储更大的数字。

Your problem is int overflow. 2147483647 is the maximum value for int. This data type can't store larger numbers.

Double应用于浮动数字。

Double should be used for floating numbers.

使用大整数使用 BigInteger 类,它是 java.math 类。只要您的计算机上有足够的内存,此课程就可以存储无限大的数字。

For using big integers use BigInteger class which is java.math class. This class can store infinitely large numbers as long as you have enough memory on your machine.

编辑:

由于您有兴趣了解 BigInteger 如何工作,我决定编辑我的答案,并向您介绍 BigInteger 秒。首先让我保证:你明白, double 不是大整数的类型吗?如果它足够你可以使用 long ,这是大于 int 的整数的类型。

As You are interested in understanding how BigInteger works I decided to edit my answer and introduce you to the world of BigIntegers. First of all let my assure: do You understand, that double isn't type for larger integers? In case it would be enough you can use long which is the type for integers larger than int.

如果 long 不够大,你应该尝试 BigInteger s 。对于巨大的 float ,有 BigDecimal 类。让我们关注 BigInteger s。

In case long isn't big enough you should try BigIntegers. For huge floats there is BigDecimal class. Let's focus on BigIntegers.

BigInteger

BigInteger 类有三个 public static 字段 BigInteger s: ONE TEN ZERO 。如果你想检查 BigInteger 是否等于0(是的, BigInteger 也可以包含小整数),那肯定会更好将其与 BigInteger.ZERO 进行比较,而不是创建新对象 BigInteger(0)

BigInteger class has three public static fields of BigIntegers: ONE, TEN and ZERO. In case you want to check if a BigInteger equals 0 (yes, BigInteger can also contain small integers) it's definitely better to compare it to BigInteger.ZERO than creating new object BigInteger(0).

构造

构造函数怎么样?最常用的有三种。第一个接受 String 作为参数,第二个接受 String int radix(数字系统的基础),第三个取位数组。

What about constructors? There are three most commonly used. The first one takes String as a parameter, the second takes a String and an int radix (base of numeric system) and the third one takes bit array.

我用来构造的最后一种方法BigIntegers 是一个名为 valueOf()公共静态方法,它取 long int 作为参数。

The last way that I use to construct BigIntegers is a public static method called valueOf() which takes long or int as a parameter.

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)) {...}

添加两个 BigInteger s a,b type

To add two BigIntegers a,b type

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

随时阅读文档非常棒!无论如何,如果您有任何问题,请随时在评论部分询问。我会回应,直到周日晚上。

Feel free to read the documentation which is great! Anyway, If You have any questions feel free to ask in the comment section. I'll respond until Sunday evening.

这篇关于使用Java的素数检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆