51nod-1186 质数检测

本文介绍了一个用于检测大整数是否为质数的算法,并提供了一段使用Java实现的示例代码。该算法通过BigInteger类的isProbablePrime方法来判断一个数是否可能是质数,返回值为true表示该数可能是质数,且概率大于95%。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接

基准时间限制:1 秒 空间限制:131072 KB 分值: 40  难度:4级算法题
 收藏
 关注
给出1个正整数N,检测N是否为质数。如果是,输出"Yes",否则输出"No"。
Input
输入一个数N(2 <= N <= 10^30)
Output
如果N为质数,输出"Yes",否则输出"No"。
Input示例
17
Output示例
Yes

boolean isPrime=aBigInteger.isprobablePrime(alpha); 
//false:  this BigInteger is 100% not prime 
//true: this BigInteger may be a prime, the probability is larger
//than (1-1/20)=95%.
//the larger the alpha, the longer the calculation time will be.

import java.util.*;
import java.math.*;
public class TEST {
   
	public static void main(String args[]){
		
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()){
			BigInteger m = cin.nextBigInteger();
			if(m.isProbablePrime(1)){
				System.out.println("Yes");
			}
			else{
				System.out.println("No");
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值