终极孪生素数

// 大家好——我现在正在测试第1000000000000000000000000000000000000个孪生素数,看看是多少哈哈
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef __int128 il;

int list[] = {2, 3, 5, 7, 19, 37, 53,  89, 113, 9973, 1000000007, 1000000009};

il ksc(il x, il y, il z)
{
	il xx = 1;
	il answer = xx * x * y % z;
	return answer;
//	if(y == 0) return 0;
//	il w = ksc(x,  y/ 2, z);
//	w= (w + w) % z;
//	if(y & 1)
//	{
//		w  = (w + x) % z;
//	}
//	return w;
}

il ksm(il x, il y, il z)
{
	if(y == 0) return 1;
	il w = ksm(x, y /2 , z);
	w= ksc(w, w, z);
	if(y & 1)
	{
		w = ksc(w, x, z);
	}
	return w;
}

bool miller_rabin(il n,il a)
{
	il d = n -1, r = 0;
	while((d & 1) == 0)
	{
		d  >>= 1;
		r ++;
	}
	il x =  ksm(a, d, n);
	if(x== 1) 
	{
		return  true;
	}
	for(il i  = 0; i  <r ; i ++)
	{
		if(x == n - 1)
		{
			return true;
		}
		x =ksc(x, x, n);
	}
	return  false;
}

bool isprime(il n )
{
	if(n < 2) return false;
	for(int a= 0;a< 12; a ++)
	{
		if(n == list[a])
		{
			return true;
		}
		if(n % list[a] == 0)
		{
			return false;
		}
		if(! miller_rabin(n, list[a]))
		{
			return false;
		}
	}
	return true;
}

bool prime(il x)
{
	return isprime(x);
}

bool ryan(__int128 x)
{
	return prime(x) && prime(x + 2);
}

inline __int128 read()
{
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}

inline void write(__int128 x)
{
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}

int main()
{
	__int128 x = read();
	__int128 cnt = 0;
	__int128 t;
	for(__int128 i = 3; ; i ++)
	{
		if(ryan(i))
		{
			cnt ++;
			t = i;
			if(cnt == x)
			{
				write(t);
				puts("");
				write(t+2);
				return 0;
			}
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值