EularProject 44:和与差均是五角数中差最小值
来源:互联网 发布:移动4g网络类型是什么 编辑:程序博客网 时间:2024/05/03 20:30
Pentagon numbers
Problem 44
Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, …
It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 − 22 = 48, is not pentagonal.
Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk − Pj| is minimised; what is the value of D?
Answer:
5482660
Completed on Tue, 25 Aug 2015, 04:45
Go to the thread for problem 44 in the forum.
#include<iostream>#include<math.h># define maxStep 10000using namespace std;long func(long n){ return n*(3 * n - 1) / 2;}bool IsPentagonal(long x){ long temp = ceil(sqrt(2.0*x / 3)); return (2 * x == 3 * temp*temp - temp) ? true : false;}bool Stop(long start, long step){ long high = func(start); long little = func(start - step); long sum = little + high; if (IsPentagonal(sum) == false) return false; long diff = high - little; if (IsPentagonal(diff)==false) return false; return diff;}void main(){ long base = func(1); long lst[maxStep], start[maxStep], step[maxStep]; for (int i = 0; i < maxStep; i++) { lst[i] = func(i + 2) - base; start[i] = i + 2; step[i] = i + 1; } while (true) { long result = Stop(start[0], step[0]); if (result) { cout << start[0] <<" "<< step[0] << endl; cout << func(start[0])-func(start[0]-step[0]) << endl; system("pause"); } start[0]++; long s1 = start[0]; long s2 = step[0]; lst[0] = func(s1) - func(s1 - s2); { long k = 1; long lst_temp = lst[0]; long start_temp = start[0]; long step_temp = step[0]; while (lst_temp > lst[k]) { lst[k-1] = lst[k]; start[k-1] = start[k]; step[k-1] = step[k]; k++; if (k == maxStep) { cout << "程序退出,因为需要增加maxStep" << endl; return; } } lst[k - 1] = lst_temp; start[k - 1] = start_temp; step[k - 1] = step_temp; } } system("pause");}
0 0
- EularProject 44:和与差均是五角数中差最小值
- EularProject 34: 一个数字与他每位数的阶乘和
- EularProject 23: 充足数的和问题
- EularProject 38:数字组合1-9分解为数字与1到n的乘积的和
- char最大值和最小值表示方法与左移右移
- [数据结构与算法]二叉树查找结点和最大最小值
- 使用异或运算和与运算求取二者最大值和最小值(不使用判断语句求二者最小值)
- 求最大值与最小值
- 找最大值与最小值
- 查询最大与最小值
- 最大值与最小值
- 最大值与最小值比较
- 数组最大值与最小值
- 最大值与最小值问题
- html 五角菜单
- 编写五角数
- QT 绘制 五角图案
- 求最大值和最小值
- Leetcode -- 3Sum Closest
- MAVEN 构建WEB项目全过程解析
- 智能运维在百度日常业务监控中的探索
- 用到过的linux 命令
- gitHub的使用
- EularProject 44:和与差均是五角数中差最小值
- Linux操作系统的安装
- 国内WiFi模组厂商盘点及发展分析
- 如果你不放弃,上帝定会来救你
- 最好的乳胶枕头ventry T3
- halcon学习笔记——(2)HDevelop language(基本数据类型)
- 基于JavaScript分析property 和 attribute
- 关于linux的历史
- 在你月薪三千的时候,做月薪八千的事