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

Problem 45

 
阅读更多

问题描述:

 

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle   Tn=n(n+1)/2   1, 3, 6, 10, 15, ...
Pentagonal   Pn=n(3n−1)/2   1, 5, 12, 22, 35, ...
Hexagonal   Hn=n(2n−1)   1, 6, 15, 28, 45, ...

It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal.

 

 

解决问题:

 

 

package projecteuler;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Problem45 {

	public static void main(String[] args){

		Map<Long, Long> number = new HashMap<Long, Long>();
		
		Long Triangle ;
		Long Pentagonal ;
		Long Hexagonal ;
		Long T = 286L;
		boolean find = false;
		do{
			Triangle = (T*(T+1))/2L;
			Pentagonal = (T*(3*T-1))/2L;
			Hexagonal = T*(2*T-1L);
			
			number.put(Hexagonal, 2L);
			if(number.containsKey(Triangle)){
				Long value = number.get(Triangle);
				if(value==1){
					find = true;
					System.out.println("Triangle:"+Triangle);
				}else{
					number.remove(Triangle);
					number.put(Triangle, 1L);
				}
			}
			if(number.containsKey(Pentagonal)){
				Long value = number.get(Pentagonal);
				if(value==1){
					find = true;
					System.out.println("Pentagonal:"+Pentagonal);
				}else{
					number.remove(Pentagonal);
					number.put(Pentagonal, 1L);
				}
			}
			System.out.println("T:"+T);
			T++;
		}while(!find);
		long r = T*(T+1)/2L;
		System.out.print(r);
	}
}

 

分享到:
评论

相关推荐

    Andrew Stankevich Contest 45 Problem analysis

    Andrew Stankevich Contest 45 Problem analysis, cf

    0-1-knapsack-problem-master (45).zip

    javaee基于ssm框架的项目

    0-1-knapsack-problem-master (45)c.zip

    打地鼠

    STM32CubeMX 5.0.0

    在Windows和Linux上:Java运行时环境(最低版本1.7.0_45) 在MacOS上:Java开发工具包(最低版本1.7.0_45) 安装STM32CubeMX 要安装STM32CubeMX,请执行以下步骤: 将最新的STM32CubeMX安装包的全部内容从...

    mynane#web-problem#杂记-45.Golang 在 Mac、Linux、Windows 下如何交叉编译1

    Mac 下编译 Linux 和 Windows 64位可执行程序Linux 下编译 Mac 和 Windows 64位可执行程序Windows 下编译 Mac

    C#,动态规划(DP)N皇后问题(N Queen Problem)的回溯(Backtracking)算法与源代码

    C#,动态规划(DP)N皇后问题(N Queen Problem)的回溯(Backtracking)算法与源代码 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的...

    Three-body-problem-PSO-INFO6205

    粒子群算法在三体问题上的实现 1.在具有图形用户界面的三体系统模拟器上实现的PSO算法,允许用户输入... 每次移动之前,我会为每个身体释放15个粒子(总共45个粒子同时运行)以找到最佳位置 此仓库已获得MIT许可: :

    Test Sequencing in Complex Manufacturing Systems

    Abstract—Testing complex manufacturing systems, such as an ASML lithographic machine, takes up to 45% of the total development time of a system. The problem of which tests must be executed in what ...

    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% ) ...

    leetcode296-LeetCode:https://leetcode-cn.com/problemset/all/

    45, , , , , , 51, 52, , , , , 57, , , , , , , , 65, , , 68, , , , , , , , 76, , , , , 81, , , 84, 85, , 87, , , , 91, , 93, , , , 97, , 99, , , , , , 105, 106, , , , , , , , , 115, 116, 117, , , , , ,...

    perl-devel-5.30.1-452.module_el8.4.0+646+45e06e4a.x86_64.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    ConferenceTrackManagement.zip

    Problem Two: Conference TrackManagement   You are planning a big programming conference and have receivedmany proposals which have passed the initial screen process but you're havingtrouble fitting...

    ACM 埃及分数 解答

    Problem 在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数。 如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的。 对于一个分数a/b,表示方法有很多种,但是哪种最好呢? 首先,加...

    Introduction_to_Optimum_Design.pdf

    Chapter 2 Optimum Design Problem Formulation 15 2.1 The Problem Formulation Process 16 2.1.1 Step 1: Project/Problem Statement 16 2.1.2 Step 2: Data and Information Collection 16 2.1.3 Step 3: ...

    程序语言设计原理习题解答

    2.3 The IBM 704 and Fortran 45 2.4 Functional Programming: LISP 52 2.5 The First Step Toward Sophistication: ALGOL 60 57 2.6 Computerizing Business Records: COBOL 63 2.7 The Beginnings of ...

    The Little Book of Semaphores

    45 3.7.1 Queue hint . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.7.2 Queue solution . . . . . . . . . . . . . . . . . . . . . . . . 49 3.7.3 Exclusive queue hint . . . . . . . . . . . . ....

    leetcode答案-ACM-Problem:C++算法

    leetcode 答案2018.9.10 2015级沉阳工业大学信息科学与工程学院计算机科学与技术算法课设 A组 1, 2题为签到题, 得出公式计算答案即可 第4题,直线分割平面的进阶版-折线分割平面,推导出公式,然后求和(实际上就是...

    leetcode338-Leetcode_Problem:leetcode记录

    问题45:跳跃游戏II 给定一个非负整数数组,您最初位于数组的第一个索引处。 数组中的每个元素代表您在该位置的最大跳跃长度。 您的目标是以最少的跳跃次数到达最后一个索引。 例子: 输入:[2,3,1,1,4] 输出:2 ...

    遗传算法解决中国旅行商问题(45个城市)

    旅行商问题( Traveling Salesman Problem , TSP) 是一个NP 完全问题, TSP 问题是组合优化领 域中的一个典型的问题. 目前求解TSP 问题的主要 方法有模拟退火算法[1 ] 、遗传算法[2 ] 、启发式搜索 法、Hopfield 神经...

    matlab代码

    % ch2problem6main.m clear; R=100; L=2e-3; C=1e-7; % 设置电路元件的参数 ...[t_out, x_out]=ode45('ch2problem6statefun',t, x0,[],R,L,C,Ts,Hs,Ls); % 仿真计算 toc s_t_simu=x_out(:,2); % 矩形波仿真结果

Global site tag (gtag.js) - Google Analytics