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 非梅森大素数的更多相关文章

  1. 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 ...

  2. Project Euler 13 Large sum

    题意:计算出以下一百个50位数的和的前十位数字. /************************************************************************* ...

  3. Python练习题 041:Project Euler 013:求和、取前10位数值

    本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # ...

  4. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  5. Python练习题 035:Project Euler 007:第10001个素数

    本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime ...

  6. Python练习题 031:Project Euler 003:最大质因数

    本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...

  7. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  8. C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)

    C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一 ...

  9. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

随机推荐

  1. NSS_11 Server Error in '/' Application

    昨天手贱在Home/Index下点了下鼠标,set as start page,然后程序一直运行不起来, 一度以为mvc的route失效了, 一直报一个错误如下:

  2. javascript面向对象--自定义类型

    Javascript是基于原型实现面向对象的,因此并没有类和接口,它的对象也与其他基于类的语言中的对象有所不同.在Javascript中,每个对象都是基于一个引用类型创建的,这个引用类型可以是原生类型 ...

  3. HTML5-Video & Audio

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. js Table冻结表头示例代码

    Table冻结表头的js实现代码. Table冻结表头: <script type="text/javascript"> //冻结table的表头  function  ...

  5. 浅析Mysql 数据回滚错误的解决方法

    介绍一下关于Mysql数据回滚错误的解决方法.需要的朋友可以过来参考下 MYSQL的事务处理主要有两种方法.1.用begin,rollback,commit来实现begin 开始一个事务rollbac ...

  6. mysql手工注入

    以下是mynona本人原创的,奉献给大家,不要小看数据库注入 参考: http://www.daydaydata.com/help/sql/advance/limit.html http://www. ...

  7. swap分区添加

    首先你需要使用命令:dd 来创建一个swapfile,然后你需要使用mkswap命令在设备或者文件中创建一个Linux swap分区a) 使用root用户登陆b) 使用下面的命令创建一个2G的 Swa ...

  8. python之input(), raw_input()

    input(): 要求输入合法的python表达式, 例如字串需要加"", 四则运算会自动计算. raw_input():所有输入视作字串 >>> val=inp ...

  9. 【ajax跨域】原因原理解决

    1.安全,跨域cookie iframe 2.很简单,就是利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的.当需要通讯时,本站脚本创建一个<scr ...

  10. 时序图 Sequence Diagram

    时序图(Sequence Diagram)是显示对象之间交互的图,这些对象是按时间顺序排列的. 时序图中显示的是参与交互的对象及其对象之间消息交互的顺序. 下面这张图介绍了时序图的基本内容: 下面这张 ...