Project Euler 45 Triangular, pentagonal, and hexagonal( 二分 + 函数指针 )
题意:
三角形数、五边形数和六角形数分别由以下公式给出:
| 三角形数 | Tn=n(n+1)/2 | 1, 3, 6, 10, 15, … |
| 五边形数 | Pn=n(3n−1)/2 | 1, 5, 12, 22, 35, … |
| 六边形数 | Hn=n(2n−1) | 1, 6, 15, 28, 45, … |
可以验证,T285 = P165 = H143 = 40755。
找出下一个同时是三角形数、五边形数和六角形数的数。
思路:因为六角形数增长速度最快,所以去枚举六角形数,然后二分一下这个数是否在三角形数和五角形数之中出现过。利用函数指针可以降低代码长度,免去了在二分中增加判断是从三角形数中查找还是五角形数中查找这一操作。
函数指针链接:****Here!
/*************************************************************************
> File Name: euler045.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月27日 星期二 14时09分07秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
typedef int64_t (*calcFunc)(int64_t); // 声明函数指针降低代码长度
// 声明了一个名为calcFunc,返回值为int64_t,函数参数列表为一个int64_t形参的指针变量
int64_t Hexagonal (int64_t n) {
return n * (2 * n - 1) ;
}
int64_t Triangle (int64_t n) {
return (n * (n + 1)) / 2;
}
int64_t Pentagonal (int64_t n) {
return (n *(3 * n - 1)) / 2;
}
bool Binary_Serch (int64_t n , calcFunc f) { // 二分查找一下n
int64_t l = 1 , r = n , mid;
while (l < r) {
mid = (l + r) >> 1;
if (f(mid) < n) l = mid + 1;
else r = mid;
}
return f(r) == n;
}
int32_t main() {
int64_t i = 143 , t;
while (cnt) {
t = Hexagonal((++i));
if (!Binary_Serch(t , Triangle)) continue;
if (!Binary_Serch(t , Pentagonal)) continue;
printf("%"PRId64"\n",t);
break;
}
return 0;
}
Project Euler 45 Triangular, pentagonal, and hexagonal( 二分 + 函数指针 )的更多相关文章
- Project Euler 363 Bézier Curves(几何+二分)
题目链接: https://projecteuler.net/problem=363 题目: A cubic Bézier curve is defined by four points: \(P_0 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- project euler 169
project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
随机推荐
- poj2299--归并排序求逆序数
/** \brief poj2299 * * \param date 2014/8/5 * \param state AC * \return memory 4640K time 3250ms ...
- jsp:attribute
在传统 JSP 中,想要实现页面布局管理比较麻烦,为了解决在 JSP 中布局的问题,出现了很多开源软件,比如 Apache Tiles 和 SiteMesh 就是其中比较优秀的.但是使用开源软件实现布 ...
- RDLC报表钻取空白页问题
在改动报表查询条件时,钻取页突然空白了,百思不得其解,之前好好的,研究了一个下午和一个晚上.查资料等等,网上非常多资料都是设置报表的 ConsumeConteinerWhitespace = True ...
- Spring Boot 版本支持对应JDK
转自:http://www.cnblogs.com/oumi/p/9241424.html 一.Spring Boot 版本支持 Spring Boot Spring Framework Java M ...
- Python使用装饰器自动调用父类__init__
众所周知,Python中class的构造函数实际是__new__(),但是如果我们执行p1=Point()的时候,不仅会调用Point的__new__方法,而且会调用Point的__init__方法. ...
- LINUX/UNIX找回删除的文件
当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹.管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志.有时可以通过lsof来恢复这些 ...
- 29. Divide Two Integers[M]两数相除
题目 Given two integers dividend and divisor, divide two integers without using multiplication, divisi ...
- Hadoop MapReduce编程 API入门系列之网页流量版本1(二十一)
不多说,直接上代码. 对流量原始日志进行流量统计,将不同省份的用户统计结果输出到不同文件. 代码 package zhouls.bigdata.myMapReduce.areapartition; i ...
- .NET使用Office Open XML导出大量数据到 Excel
我相信很多人在做项目的都碰到过Excel数据导出的需求,我从最开始使用最原始的HTML拼接(将需要导出的数据拼接成TABLE标签)到后来happy的使用开源的NPOI, EPPlus等开源组件导出EX ...
- Dalvik 堆内存管理与回收
Dalvik虚拟机用来分配对象的堆划分为两部分,一部分叫做Active Heap,另一部分叫做Zygote Heap.下面基于管理机制来介绍为何分配为这两部分,以及堆内存的管理. 我们从Android ...