欧拉计划 第四十五题

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
Triangle    T*n*=*n*(*n*+1)/2        1, 3, 6, 10, 15, ...  
Pentagonal P*n*=*n*(3*n*−1)/2    1, 5, 12, 22, 35, ... 
Hexagonal  H*n*=*n*(2*n*−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.
三角形,五角形和六角形数字由以下公式生成:
 三角形       T *n* = *n*(*n* +1)/ 2          1,3,6,10,15 ......  
 五角形       P *n* = *n*(3 *n* -1)/ 2       1,5,12,22,35 ......  
 六角形       H *n* = *n*(2 *n* -1)           1,6,15,28,45,...... 
可以证实T 285 = P 165 = H 143 = 40755。

找到下一个满足三角形 五角形 六角形的数字。

#include <stdio.h>
#include <inttypes.h>

int is_Pentagonal(int64_t x){
	int64_t min = 1, max = x, mid;
	while(min <= max){
		mid = (min + max) / 2;
		int64_t pen = mid * (3 * mid - 1) / 2;
		if(pen == x) return 1;
		if(pen < x) min = mid + 1;
		else max = mid - 1; 
	}
	return 0;
}

int main(){
	int64_t n = 145;
	while(!is_Pentagonal(n * (2 * n - 1))) n++;
	printf("%"PRId64"\n", n * (2 * n - 1));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38362049/article/details/80953188
今日推荐