Project Euler 97 :Large non-Mersenne prime 非梅森大素数
The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 26972593−1; it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2p−1, have been found which contain more digits.
However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433×27830457+1.
Find the last ten digits of this prime number.
1999年人们发现了第一个超过一百万位的素数,这是一个梅森素数,可以表示为26972593−1,包含有2,098,960位数字。在此之后,更多形如2p−1的梅森素数被发现,其位数也越来越多。
然而,在2004年,人们发现了一个巨大的非梅森素数,包含有2,357,207位数字:28433×27830457+1。
找出这个素数的最后十位数字。
解题
感觉很简单。。。
JAVA
package Level3; import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList; public class PE097{
public static void run() {
BigInteger m = new BigInteger("10000000000");
BigInteger r1 = new BigInteger("28433");
BigInteger t = new BigInteger("2");
BigInteger exp = new BigInteger("7830457");
BigInteger res = t.modPow(exp, m);
res = r1.multiply(res).add(new BigInteger("1"));
res = res.mod(m);
System.out.println(res);
}
public static void main(String[] args) throws IOException {
long t0 = System.currentTimeMillis();
run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms"); }
}
// 8739992577
// running time=0s2ms
就这样
或者这样
public static void run2(){
long base = 2;
long mod = 1000000000;
long exp = 7830457;
long res = 28433;
for(long i =1;i<=exp;i++){
res = (res*2)%mod;
}
res +=1;
res %=mod;
System.out.println(res);
}
// 739992577
// running time=0s163ms
上面mod少个0求的是后9位的数,因为多个0就越界了,少一位手工0到9可以暴力遍历。。。
Python
# coding=gbk
import copy
import time as time
def main():
print ((28433*(2**7830457))+1)%10000000000
t0 = time.time()
main()
t1 = time.time()
print "running time=",(t1-t0),"s"
#
# running time= 0.0190000534058 s
也就这样
Project Euler 97 :Large non-Mersenne prime 非梅森大素数的更多相关文章
- Project Euler:Problem 41 Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...
- Project Euler 13 Large sum
题意:计算出以下一百个50位数的和的前十位数字. /************************************************************************* ...
- Python练习题 041:Project Euler 013:求和、取前10位数值
本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 035:Project Euler 007:第10001个素数
本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime ...
- Python练习题 031:Project Euler 003:最大质因数
本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)
C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
随机推荐
- 33选6算法:M个数N个为一组,无重复的排列组合
private void button1_Click(object sender, EventArgs e) { int nCnt = 0; List nNumList = new List(); f ...
- 用PHP判断客户端是否是手机
<?php function isMobile(){ $useragent = isset($_SERVER['HTTP_USER_AGENT'])? $_SERVER['HTTP_USER_A ...
- TCP HelloWord
client.cpp #include <stdio.h> #include <winsock2.h> #pragma comment (lib,"ws2_32&qu ...
- 解决DataSnap支持的Tcp长连接数受限的两种方法
如何解决DataSnap支持的Tcp长连接数受限的问题? 方案一: 采用代理服务器方式,基本流程为: 1.客户先连接代理服务器:2.获取可用的服务器IP和端口:3.关闭与代理服务器之间的连接:4.建立 ...
- JMS之开源实现ActiveMQ
1.ActiveMQ是开源的JMS实现. 可以把不影响用户执行结果又比较耗时的任务(比如发邮件通知管理员)异步的扔给jms 服务端,而尽快的把屏幕返还给用户,且服务端能够多线程排队响应高并发的请求.可 ...
- MyException
自定义Exception using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- snmptrap使用
SNMP简单网络管理协议,其中其支持的一个命令snmptrap命令,用于模拟向管理机发送trap消息. 启动陷阱方法: snmptrapd -C -c /etc/snmp/snmptrapd.co ...
- IOS开发经验分享
一些IOS开发的心得: 1) [Multiple Threads] IOS多线程注意, 所有的UI操作都必须在主线程上: Any code that will update the UI should ...
- sharepoint 认证
MCPD http://www.microsoft.com/learning/en/us/mcpd-certification.aspx#fbid=YktyKIYXeFg Exam 70-573: T ...
- c++ 函数返回指针 及用法
#include<string> #include<iostream> using namespace std; string fun1(int a) { string str ...