project euler 37
2015-12-04 21:17
253 查看
Problem
37
Truncatable primesThe number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
可截素数
3797有着奇特的性质。不仅它本身是一个素数,而且如果从左往右逐一截去数字,剩下的仍然都是素数:3797、797、97和7;同样地,如果从右往左逐一截去数字,剩下的也依然都是素数:3797、379、37和3。
只有11个素数,无论从左往右还是从右往左逐一截去数字,剩下的仍然都是素数,求这些数的和。
注意:2、3、5和7不被视为可截素数。
@Test public void test() { CalTruncatablePrime truncateUtilLeft = new CalTruncatablePrime(); truncateUtilLeft.iterSet(new HashSet<Long>( truncateUtilLeft.getTruncatePrimes()), TRUNCATE_DIR.LEFT); System.out.println(truncateUtilLeft.getTruncatePrimes()); System.out.println(truncateUtilLeft.getTruncatePrimes().size()); CalTruncatablePrime truncateUtilRt = new CalTruncatablePrime(); truncateUtilRt.iterSet(new HashSet<Long>( truncateUtilRt.getTruncatePrimes()), TRUNCATE_DIR.RIGHT); System.out.println(truncateUtilRt.getTruncatePrimes()); System.out.println(truncateUtilRt.getTruncatePrimes().size()); long sum = 0; for(Long val : truncateUtilLeft.getTruncatePrimes()){ if( truncateUtilRt.getTruncatePrimes().contains(val)){ System.err.println(val); sum += val; } } System.out.println("sum=" + (sum - 2 - 3 - 5 - 7)); } public static enum TRUNCATE_DIR { LEFT, RIGHT } public static class CalTruncatablePrime { private Set<Long> truncatablePrimes = new HashSet<Long>(); public CalTruncatablePrime() { truncatablePrimes.add(2L); truncatablePrimes.add(3L); truncatablePrimes.add(5L); truncatablePrimes.add(7L); } public Set<Long> getTruncatePrimes() { return truncatablePrimes; } public void iterSet(Set<Long> candidates, TRUNCATE_DIR direction) { if (candidates == null || candidates.size() < 1) { return; } Set<Long> newCandidates = new HashSet<Long>(); for (Long val : candidates) { for (int i = 1; i <= 9; i++) { int bit = val.toString().length(); long newVal = val; if (direction.equals(TRUNCATE_DIR.LEFT)) { newVal += Math.pow(10, bit) * i; } else { newVal = newVal * 10 + i; } if (isPrime(newVal)) { newCandidates.add(newVal); System.out.println(newVal); truncatablePrimes.add(newVal); } } } if (newCandidates.size() < 1) { return; } iterSet(newCandidates, direction); } private boolean isPrime(long val) { long num = Math.abs(val); if (num <= 10) { if (num == 2 || num == 3 || num == 5 || num == 7) { return true; } return false; } else { for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { return false; } } return true; } } }
相关文章推荐
- LibSVM C/C++
- hud 2099
- PHPCMS几个有用的全局函数
- hdu1176
- java获取系统时间比实际时间少了8个小时的解决方法
- NYOJ 119士兵杀敌(三)【RMQ裸题】
- EF--.net
- [leetcode] Ugly Number II
- otsu算法---matlab实现,和一种改进算法
- 解压版的Tomcat基本配置和安装
- 双向循环链表的实现
- 大数据时代:生活、工作与思维的大变革(上)
- 复杂链表的复制(C++)
- 一个操作系统的实现(1):分析linux下如何运行一个执行文件
- 开通博客啦
- OC中的方法
- project euler 36
- Android消息机制原理详解(Looper、Handler、MessageQueue)
- php中urldecode()和urlencode()
- 新鲜出炉:米4刷win10遇到的各种问题解答(个人版)