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 ...
随机推荐
- 自己手写简约实用的Jquery tabs插件(基于bootstrap环境)
一直想改版网站首页的图书展示部分,以前的展示是使用BootStrap的传统的collapse,网页篇幅占用大,也不够美观,操作也相对来说比较麻烦.于是有了自己利用Jquery来做一个图书展示的tabs ...
- $(document).height()、$("body").height()、$(window).height()区别和联系
前言:在此以高度为示例,宽度问题可类推.在移动端开发中,经常遇到需要把一块内容定位于底部的情况,当页面内容不满一屏时,需要设为fixed,而超过 一屏时,需要设为static随页面顶到底部,此时就需要 ...
- 解决前端浏览器传JSON对像到服务端后全部变成string类型的方法
这几天公司用nodejs+mongodb来做些东西,UI用的是kendo UI 碰到一个问题: 举个例子var a={"name":"张三","age ...
- Global::validateEmail
/***************************************************************** (C) Copyright DENTSPLY Internatio ...
- 使用PowerShell 连接Azure
除了使用门户登入外,还可以使用PowerShell的方式来连接Azure.首先要去下载组件 http://azure.microsoft.com/en-us/downloads/?rnd=1 http ...
- tcp-client-c++
#include "stdafx.h" #include <Winsock2.h> #include <iostream> #pragma comment( ...
- Setting composer minimum stability for your application
Do you have a confusion of how do you determine the stability when using composer dependency manager ...
- div+css布局细节问题
cursor: pointer;在chrome里支持,hand不支持
- Could not find file '..\bin\hibernate.cfg.xml'解决方法:
web.config: 解决方法:
- 活动 Activity 四种加载模式
singleTop要求如果创建intent的时候栈顶已经有要创建的Activity的实例,则将intent发送给该实例,而不发送给新的实例.(注意是栈顶,不在栈顶照样创建新实例!) singleTas ...