[英]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
。 2147483647
是int
的最大值。 此数据类型不能存储更大的数字。
Double应该用于浮点数。
要使用大整数,请使用BigInteger
类,它是java.math
类。 只要您的计算机上有足够的内存,此类就可以存储无限大的数字。
编辑:
当您有兴趣了解BigInteger
工作原理时,我决定编辑答案,并向您介绍BigInteger
的世界。 首先让我保证:您了解吗,不是为大整数键入double
吗? 万一足够,您可以使用long
,它是大于int
整数的类型。
如果long
不够long
,则应尝试使用BigInteger
。 对于巨大的float
有BigDecimal
类。 让我们专注于BigInteger
。
的BigInteger
BigInteger
类有三个public static
字段BigInteger
S: ONE
, TEN
和ZERO
。 如果要检查BigInteger
等于0(是的, BigInteger
也可以包含小整数),则将其与BigInteger.ZERO
进行比较肯定比创建新对象BigInteger(0)
更好。
施工
构造函数呢? 最常用的有三种。 第一个使用String
作为参数,第二个使用String
和一个int
基数(数字系统的基数),第三个使用位数组。
我用来构造BigIntegers
的最后一种方法是一个称为valueOf()
的public static
方法,该方法需要long
或int
作为参数。
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
的a,b
类型
BigInteger c = a.add(b);
//or
a = a.add(b);
随意阅读文档 ,很棒! 无论如何,如果您有任何问题,请随时在评论部分提出。 我会回复到周日晚上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.