1015 Reversible Primes (20分)

本文介绍了一个C++程序,用于判断输入的整数是否为素数,并检查其逆序数是否同样为素数。程序包含素数判断、逆序数计算等功能,通过输入两个整数a和b,输出a是否为素数及其逆序数是否仍为素数的结果。

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

//素数判断刚开始没写全,少了个2的情况.... 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

bool isPrime(int a){
	int end = sqrt(a);
	if(a == 2){
		return true;
	}
	if(a == 1 || a%2 == 0)
		return false;
		
	for(int i=3; i<=end; i+=2){
		if(a%i == 0){
			return false;
		}
	}return true;
}
int  Reverse(int a, int r){
	int res = 0;
	while(a){
		res *= r;
		res += (a%r);
		a /= r;
	}
	return res;
}
int main()
{
	int a, b;
	
	while(1){
		cin >> a;
		
		if(a <0)
		return 0;
		
		cin >> b;
		if(!isPrime(a)){
			cout << "No" << endl;
			continue;
		}
		
		int res = Reverse( a,  b);
	//	cout << res << endl;
		if(!isPrime(res)){
			cout << "No" << endl;
			continue;
		}else cout << "Yes" << endl;
		
	}
	return 0;
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值