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 ...
随机推荐
- C#操作Excel基本操作
/// using Microsoft.Office.Core; using Microsoft.Office.Interop.Excel; using System.IO; using System ...
- WinForm 实现验证码
private void CheckIdentifyingCode() { Random r = new Random(); string str = ""; for (int i ...
- 跨域名设置cookie或获取cookie
可以使用jquery里面的ajax中的jsonp的方式来访问就可以了.代码如下: $.ajax({ url: 'your url', data: {'xx' : 'xx', 'xx2' : 'xx2' ...
- RichTextBox 自动滚动到最后
RichTextBox.AppendText($"[{DateTime.Now.ToString("hh:mm:ss")}] {msg}\n"); RichTe ...
- 二,WPF的布局
所有WPF布局窗口都派生自System.WIndows.Controls.Panel抽象类的面板. 不能在布局容器中放置字符串内容,而是需要一个继承自UIElement的类对字符串进行包装,如Text ...
- 《.NET简单企业应用》项目开发环境
项目开始,开发团队需要构建一套开发环境,主要包含:开发工具.代码管理/版本控制系统.任务和Bug管理系统和持续集成(CI)系统.本文主要列举项目开发中经常使用的开发工具和第三方库. 本文所列工具根据前 ...
- Java从入门到精通——技巧篇之利用dom4j取出XML文件中的数据
在我们做项目的时候会经常用到XML文件用来配置系统,XML让系统更加的具有了灵活性,Java如何从XML中取出我们想要的数据呢?下面是我利用DOM4J来实现取出XML文件中的数据. XML文件 < ...
- [Java][RCP] 引入第三方jar包时出错: XXXcannot be found XXX
为什么会这样? 下面的博客有介绍,不在累赘 http://dengmin.iteye.com/blog/260585 这些博客貌似忘掉了一点,或者是我本地的Eclipse新建的项目Version不够高 ...
- Android 中的WiFi剖析
Android的WiFi 我们通常看到WiFi的守护进程wpa_supplicant在我们的ps的进程列表中,这个就是我们的wifi守护进程.wpa_supplicant在external/wpa_s ...
- STL算法
STL算法部分主要由头文 件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<algorit ...