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 × ...
随机推荐
- /tmp目录下执行脚本失败提示Permission denied
Linux上执行Shell脚本运行失败提示Permission denied一个问题,挺好的问题,切中了知识盲点. 问题现象 Shell脚本在/tmp目录下,执行./test.sh运行失败,提示Per ...
- [SCSS] Convert SCSS Variable Arguments to JavaScript
We will learn how to convert variable arguments by using rest operator in JavaScript. .sass-btn { co ...
- 输入url发生了什么--前端所有知识
面试经常会问到的一个问题,这个问题舒展开来,其实包含了前端(一些后端)几乎所有的知识.梳理一下,备忘.包含了一些面经中常问的问题. 有时间待续
- Ubuntu下启动Eclipse报错:A Java RunTime Environment (JRE) or Java Development Kit (JDK) must
原以为是jdk的环境变量配置错误了,于是从网上找了各种配置环境变量的方法.也注意空格的问题,可无论怎么改,还是这样报错!后来在网上看到一种奇怪的方法.我也不知道为什么这样就OK了? 方法:进入你的ec ...
- 【cl】控制台执行Java程序
1.设置CLASSPATH环境变量,指向package所在的目录,一般是项目文件夹的bin目录 2.执行 java package.ClassName linux: 方法一: 步骤1:export ...
- Jenkins project
1.project name 这个作为git clone的target folder 2.Multiple SCMs 建立2个git类型的操作,相互独立. additional Behaviors 设 ...
- SQLite 常用函数
SQLite 常用函数 参考: SQLite 常用函数 | 菜鸟教程http://www.runoob.com/sqlite/sqlite-functions.html SQLite 常用函数 SQL ...
- 写个js 分页玩玩(原创)
<ul id="page"> <li class="pagetest">1</li> <li class=" ...
- Jsoup的简单的使用示例
利用Jsoup中的相关方法实现网页中的数据爬去,本例子爬去的网页为比较流行的programmableweb中的mashup描述内容,然后为数据库中存在的mashup添加相应的描述. package c ...
- mysql.connector 事务总结
mysql.connector事务总结: connection.autocommit = 0 (默认值) 事务处理 使用 connection.commit()方法 #!/usr/bin/env py ...