欧拉工程第60题:Prime pair sets
五个数,任意两个数的任意链接后的数还是质数
满足这个条件的最小五个数的和是多少?
结果:26033
纯暴力破解:
package projecteuler51to60; import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet; class level60{
void solve1(){
List<Integer> primes = new ArrayList<>();
int Max = 10000; boolean flag=true;
primes.add(3);
int nextPrime = 3;
while(primes.size()!=5){
// System.out.println(nextPrime); nextPrime = nextPrimes(nextPrime);
flag=true;
for(int i=0;i<primes.size();i++){
if(concatPrime(primes.get(i),nextPrime) ==false){
flag = false;
}
}
if(nextPrime>Max && flag==true){
primes.add(nextPrime);
// System.out.println(nextPrime);
}
if(nextPrime>Max){
System.out.println(primes.size());
int temp = primes.remove(primes.size()-1);
primes.add(temp);
}
}
int sum=0;
for(int i=0;i<primes.size();i++){
sum+=primes.get(i);
System.out.println(primes.get(i)+" ");
}
}
int nextPrimes(int a){
for(int i = a+1;;i++)
if(isPrime(i))
return i;
}
void solve0(){
int Max =10000;
boolean[] primeArray = new boolean[Max]; for(int i=1;i<Max;i++)
if(isPrime(i))
primeArray[i]= true;
else primeArray[i]=false;
for(int i=3;i<Max;i=i+2){
int a=i;
if(isPrime(a)){
for(int b=a+2;b<Max;b=b+2){
if(isPrime(b) &&concatPrime(a,b)){
for(int c=b+2;c<Max;c=c+2){
if(isPrime(c) && concatPrime(a,c) &&concatPrime(b,c)){
for(int d=c+2;d<Max;d=d+2){
if(isPrime(d)&&concatPrime(a,d) &&concatPrime(b,d) &&concatPrime(c,d)){
for(int e=d+2;e<Max;e=e+2){
if(isPrime(e) &&concatPrime(a,e) &&concatPrime(b,e) &&concatPrime(c,e)&&concatPrime(d,e)){
System.out.println(a+b+c+d+e);
System.out.println(a+" "+b+" "+c+" "+d+" "+e);
return;
}
}
}
}
}
}
}
}
}
}
}
boolean concatPrime(int a,int b){
String digit1=String.valueOf(b)+String.valueOf(a);
String digit2=String.valueOf(a)+String.valueOf(b);
if(isPrime(Integer.parseInt(digit1)) &&isPrime(Integer.parseInt(digit2)))
return true;
return false;
} boolean isPrime(int num){
if(num==2||num==3 ||num==5||num==7) return true;
if(num<2 || num%2==00) return false;
for(int i=3;i<=Math.sqrt(num);i++)
if(num%i==0)
return false;
return true;
} }
public class Problem60 { public static void main(String[] args){
long begin= System.currentTimeMillis();
new level60().solve0();
long end = System.currentTimeMillis();
long Time = end - begin;
System.out.println("Time:"+Time/1000+"s"+Time%1000+"ms");
} }
欧拉工程第60题:Prime pair sets的更多相关文章
- 欧拉工程第69题:Totient maximum
题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- 欧拉工程第51题:Prime digit replacements
题目链接 题目: 通过置换*3的第一位得到的9个数中,有六个是质数:13,23,43,53,73和83. 通过用同样的数字置换56**3的第三位和第四位,这个五位数是第一个能够得到七个质数的数字,得到 ...
- 欧拉工程第54题:Poker hands
package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...
- 欧拉工程第67题:Maximum path sum II
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- 欧拉工程第65题:Convergents of e
题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...
- 欧拉工程第74题:Digit factorial chains
题目链接:https://projecteuler.net/problem=74 数字145有一个著名的性质:其所有位上数字的阶乘和等于它本身. 1! + 4! + 5! = 1 + 24 + 120 ...
- 欧拉工程第56题:Powerful digit sum
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...
随机推荐
- MongoDB如何存储数据
想要深入了解MongoDB如何存储数据之前,有一个概念必须清楚,那就是Memeory-Mapped Files. Memeory-Mapped Files 下图展示了数据库是如何跟底层系统打交道的. ...
- 51nod贪心算法入门-----完美字符串
约翰认为字符串的完美度等于它里面所有字母的完美度之和.每个字母的完美度可以由你来分配,不同字母的完美度不同,分别对应一个1-26之间的整数. 约翰不在乎字母大小写.(也就是说字母F和f)的完美度相同. ...
- 单元测试SimpleTest新手入门
最近学习单元测试,先用了下PHPunit,结果安装问题一大堆,于是立刻放弃改试simpletest,感觉简单多了.下面列出步骤. 1.下载simpletest(版本1.1.0), http://www ...
- apk反编译之二——smali学习
在apk被反编译后,原来的java程序会以smali文件呈现.这就需要补充smali的知识.依旧参考官方文档,择日我将把官方文档做一下翻译.今日先贴出链接地址: 1:了解smali字节码的寄存器 请参 ...
- MongoDB Long/Int(长整型)的自增长主键 解决方案
今朝有幸尝芒果,发现自增长ID类型有多种,唯独没有Long/Int. 一思路:1. 自建一个Collection(表,假设名为:IdentityEntity,其中字段:_id, Key, Value, ...
- js实现复制到剪切板
// <![CDATA[ function copy_clip(copy) { if (window.clipboardData) { window.clipboardData.setData( ...
- GridView中的荧光棒效果
使用 ASP.NET中的GridView控件的时候会遇到这个效果,当时觉得很神奇,其实就是两句代码的事儿,可是时间长了,有点儿忘了,今天练习一下, 顺便把删除的时候弹出js中的confirm对话框也写 ...
- 在有跳板机的情况下,SecureCRT自动连接到目标服务器
为了服务器的安全,运维人员经常会要求我们先登录到跳板机,然后再SSH连接到目标服务器.但是这样是很繁琐的,每次在SecureCRT创建一个连接,都需要输入SSH命令,然后输入密码. 下面的方法可以实现 ...
- 1105. Spiral Matrix (25)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- .NET中 MEF应用于IOC
IOC解释 IOC,控制反转的意思.所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓反转,你必须理解如果不反转,会怎么着,因为A必须要有B,才可 ...