`
to_zoe_yang
  • 浏览: 143322 次
  • 性别: Icon_minigender_2
  • 来自: 01
社区版块
存档分类
最新评论

Problem 37

 
阅读更多

问题描述:

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

 

 

解决问题:

package projecteuler;

import java.util.Arrays;

public class Problem37 {

	public static boolean prime[] ;
	public static int UP = 11;
	
	public static boolean IsPrime(int number) {
		boolean result = true;

		if(number==1||number==2){
			return true;
		} else if (number % 2 == 0) {
			return true;
		} else {
			int middle = (int) Math.sqrt(number);
			for (int i = 3; i <= middle; i += 2) {
				if (number % i == 0) {
					return false;
				}
			}
		}
		return true;
	}

	public static boolean IsNumber(int number){
		
		int tmp = number;
		int max = 10;
		int n;
		do{
			n = tmp%max;
//			System.out.println("1:"+tmp%max);
			if(!prime[n]){
				return false;
			}
			max = max*10;
		}while(n!=number);
		max = max/100;
//		System.out.println("Max:"+max);
		do{
			n = tmp/max;
//			System.out.println("2:"+tmp/max);
			if(!prime[n]){
				return false;
			}
			max = max/10;;
		}while(n!=number);
		return true;

	}
	
	public static void main(String[] args){
		long t1 = System.currentTimeMillis();
		prime = new boolean[3000000];
		Arrays.fill(prime, false);
		for (int i = 3; i < 3000000; i=i+2) {
			if(IsPrime(i))
				prime[i] = true;
		}
		prime[2] = true;
		int n = 11;
		int count = 0;
		int sum = 0;
		do{
			if(IsNumber(n)){
				sum +=n;
				count++;
				System.out.println(n);
			}
			n = n+2;
		}while(count<UP);
		long t2 = System.currentTimeMillis();
		System.out.println("Sum:"+sum);
		
		System.out.println(t2-t1);
	}
}

 

 

 

分享到:
评论

相关推荐

    爱立信CSR数据采集规范

    23. MGW related problem 37 23.1 Collect Configuration Data (CV) 37 23.2 Collect Alarm, Event, Availability and System Logs 37 23.3 C Collect Trace & Error logs, Processor loads and Disk space 37

    Josephus problem_Josephusproblem_K._

    约瑟夫环问题,也称为约瑟夫斯问题或Josephus Problem,是一个著名的理论问题,源自公元37年的犹太历史。在这个问题中,人们按照一定的规则从一个圆圈中淘汰,直到只剩下一个或者几个参与者。问题的核心在于找出在...

    Python库 | pygmo-2.9-cp37-cp37m-manylinux1_x86_64.whl

    《Python库pygmo-2.9-cp37-cp37m-manylinux1_x86_64.whl深度解析》 Python作为一种高级编程语言,拥有丰富的库支持,使得开发者可以高效地进行各种复杂任务的处理。其中,`pygmo`库是一个面向全局优化问题的开源...

    ifs-37-ifs190294.pdf

    regulation problem by system transformation. Then the optimal regulation problem is solved by the policy iteration adaptive dynamic programming algorithm. The policy iteration algorithm is the ...

    A Sequential Bundle Method for Solving a MPEC Problem

    本文主要探讨了一类凸数学规划问题,即带有不可微凸目标函数和约束条件分离为两个变量向量的数学规划问题,其中第二个变量向量属于约束子问题的最优解集。文章介绍了一种序列束方法来解决这类问题,并对其进行了收敛...

    Python库 | pyvroom-0.0.6-cp37-cp37m-win_amd64.whl

    `pyvroom`是一个基于Python的库,它为VROOM,一个开源的车辆路径问题(Vehicle Routing Problem, VRP)求解器提供了接口。VRP是一类优化问题,旨在有效地规划多辆车辆的路线,以满足客户的需求,同时最小化总行驶...

    leetcode-problem-solving:LeetCode问题解决

    Problem Runtime Mem Usage Level 28毫秒( 93.73% ) 13.4 MB( 79.11% ) 简单 52毫秒( 94.00% ) 13.6 MB( 45.02% ) 中等的 36毫秒( 95.77% ) 13.7 MB( 73.48% ) 中等的 68毫秒( 88.56% ) ...

    Python库 | multiset_multicover-0.3-cp37-cp37m-win32.whl

    多覆盖问题(Multicover Problem)是一个经典的组合优化问题,它的目标是在给定的一组物品(多重集元素)和它们的权重(需求量)的情况下,找到最小数量的集合,使得每个元素至少被覆盖一次。这个问题在资源分配、...

    ANSI_E1-20&E1;-37-1&E1;-37-2_RDM.pdf

    The 2010 edition corrects errors in the 2006 edition, and adds a new message to say that a previously reported problem has been cleared. The 2010 edition is bookmarked for easier use. The download ...

    Hadoop in Practice(2012)

    2012 | ISBN-10: 1617290238 | PDF, EPUB | 536 pages | 26 + 37 MB Hadoop in Practice collects 85 Hadoop examples and presents them in a problem/solution format. Each technique addresses a specific ...

    多元个性化需求驱动的最后一公里配送选址-路径研究.pdf

    基于这些需求,他们构建了一个选址-路径问题(Location-Routing Problem, LRP),该问题旨在最小化运营成本,同时满足客户的个性化要求。为了解决这个复杂问题,他们设计了一种改进的变邻域搜索算法(Variable ...

    The Little Book of Semaphores

    37 3.6.4 Reusable barrier hint . . . . . . . . . . . . . . . . . . . . . 39 3.6.5 Reusable barrier solution . . . . . . . . . . . . . . . . . . 41 3.6.6 Preloaded turnstile . . . . . . . . . . . . . ...

    leetcode-problem:leetcode中问题的解决方案

    1-50(数量:37) 26从排序的数组中删除重复项.md 27-删除元素.md 28-实现-strstr.md 第29章二分一体.md 3个最长的子字符串,没有重复的字符.md 33.在旋转排序数组中搜索.md 34在排序数组中找到元素的第一个...

    Handbook.of.Graph.Theory.Combinatorial.Optimization.and.Algorithms.158

    CHAPTER 37 - ε-Approximation Schemes for the Constrained Shortest Path Problem CHAPTER 38 - Constrained Shortest Path Problem: Lagrangian Relaxation-Based Algorithmic Approaches CHAPTER 39 - ...

    2024美赛资料 -美赛一本通

    其中,共有MCM 有22支队伍,ICM 有15 支队伍荣获全球特等奖,累计37支队伍荣获全球特等奖,并且34支队伍全球特等奖来自中国队,3支全球特等奖来自美国队。几乎所有题目的全球特等奖都被中国队包揽了,中国队太厉害了...

    Discriminative Scale Space Tracking.pdf

    —Accurate scale estimation of a target is a ... Our method obtains the top rank in performance by outperforming 19 state-of-the-art trackers on OTB and 37 state-of-the-art trackers on VOT2014.

    计算机网络第六版答案

    Computer Networking: A Top-Down Approach, 6th Edition Solutions to Review Questions and Problems Version Date: May 2012 ...This document contains the solutions to review questions ...Problem 1 There...

Global site tag (gtag.js) - Google Analytics