【ProjectEuler】ProjectEuler_044

2024-03-06 12:58
文章标签 044 projecteuler

本文主要是介绍【ProjectEuler】ProjectEuler_044,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#pragma once
#include <windows.h>
class MoonMath
{
public:
MoonMath(void);
~MoonMath(void);
//************************************
// Method:    IsInt
// Access:    public
// Describe:  判断double值在epsilon的范围内是否很接近整数
//            如1.00005在epsilon为0.00005以上就很接近整数
// Parameter: double doubleValue    要判断的double值
// Parameter: double epsilon        判断的精度,0 < epsilon < 0.5
// Parameter: INT32 & intValue      如果接近,返回最接近的整数值
// Returns:   bool                  接近返回true,否则返回false
//************************************
static bool IsInt(double doubleValue, double epsilon, INT32 &intValue);
//************************************
// Method:    Sign
// Access:    public
// Describe:  获取value的符号
// Parameter: T value   要获取符号的值
// Returns:   INT32     正数、0和负数分别返回1、0和-1
//************************************
template <typename T>
static INT32 Sign(T value);
//************************************
// Method:    IsPrimer
// Access:    public
// Describe:  判断一个数是否是素数
// Parameter: UINT32 num    要判断的数
// Returns:   bool          是素数返回true,否则返回false
//************************************
static bool IsPrimer(UINT32 num);
//************************************
// Method:    IsIntegerSquare
// Access:    public static
// Describe:  判断给定的数开平方后是否为整数
// Parameter: UINT32 num
// Returns:   bool
//************************************
static bool IsIntegerSquare(UINT32 num);
};


 

#include "MoonMath.h"
#include <cmath>
MoonMath::MoonMath(void)
{
}
MoonMath::~MoonMath(void)
{
}
template <typename T>
INT32 MoonMath::Sign(T value)
{
if(value > 0)
{
return 1;
}
else if(value == 0)
{
return 0;
}
else
{
return -1;
}
}
bool MoonMath::IsInt(double doubleValue, double epsilon, INT32 &intValue)
{
if(epsilon > 0.5 || epsilon < 0)
{
return false;
}
if(INT32(doubleValue + epsilon) == INT32(doubleValue - epsilon))
{
return false;
}
INT32 value = INT32(doubleValue);
intValue = (fabs(doubleValue - value) > 0.5) ? (value + MoonMath::Sign(doubleValue)) : (value) ;
return true;
}
bool MoonMath::IsPrimer(UINT32 num)
{
// 0和1不是素数
if(num <= 1)
{
return false;
}
UINT32 sqrtOfNum = (UINT32)sqrt((double)num); // num的2次方
// 从2到sqrt(num),如果任何数都不能被num整除,num是素数,否则不是
for(UINT32 i = 2; i <= sqrtOfNum; ++i)
{
if(num % i == 0)
{
return false;
}
}
return true;
}
bool MoonMath::IsIntegerSquare(UINT32 num)
{
UINT32 qurtNum = (UINT32)sqrt((double)num);
return (qurtNum * qurtNum) == num;
}


 

//  Pentagon numbers
// 	Problem 44
// 	Pentagonal numbers are generated by the formula, Pn=n(3n1)/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 is pentagonal and D = |Pk  Pj| is minimised; what is the value of D?
#include <iostream>
#include <windows.h>
#include <ctime>
#include <assert.h>
#include <string>
#include <MoonMath.h>
using namespace std;
//************************************
// Method:    CalcPentagonNum
// Access:    public
// Describe:  计算第n个Pentagon数
// Parameter: UINT32 n
// Returns:   UINT32
//************************************
inline UINT64 CalcPentagonNum(UINT32 n)
{
return UINT64(n) * (3 * n - 1) / 2;
}
//************************************
// Method:    IsPentagonNum
// Access:    public
// Describe:  判断num是否是Pentagon数
// Parameter: UINT32 num    要判断的Pentagon数
// Parameter: UINT32 &n     如果是Pentagon数,返回n,否则返回无效值
// Returns:   bool          是Pentagon数返回true,否则返回false
//************************************
bool IsPentagonNum(UINT64 num, UINT32 &n)
{
// 根据一元二次方程求解公式求出n的值
// 3*n^2-n-2*num=0
// n=(1+-sqrt(1+24*num))/6
// 因为n>0
// 所以n=(1+sqrt(1+24*num))/6
double nDouble = (1.0 + sqrt(1.0 + 24.0 * num)) / 6;
const double EPSILON = 0.00001; // 判断n是否为整数的精确度
n = 0;
// 如果n是整数,则视计算Pentagon的公式可逆推,num为Pentagon数
if(!MoonMath::IsInt(nDouble, EPSILON, (INT32 &)n))
{
return false;
}
return CalcPentagonNum(n) == num;
}
void TestFun1()
{
UINT32 n = 0;
const UINT32 MAX_TEST_N = 100000000;
for(UINT32 i = 0; i < MAX_TEST_N; i++)
{
UINT64 pI = CalcPentagonNum(i);
if(!IsPentagonNum(pI, n))
{
cout << "i = " << i << endl << "CalcPentagonNum(i) = " << i << endl;
}
}
cout << "Test OK!";
}
void F1()
{
cout << "void F1()" << endl;
// TestFun1();
LARGE_INTEGER timeStart, timeEnd, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&timeStart);
/*********************************算法开始*******************************/
// d < j < k < x
// pD = p(d)
// pJ = p(J)
// p(k) - p(j) = p(d)
// p(k) + p(j) = p(x)
// p(d) >= p(j+1) - p(j)    否则p(j)+p(d)肯定不会是下一个Pentagon数
// 令m(n) = p(n+1) - p(n)
const UINT32 START_N = 1;
bool haveFound = false;
UINT32 j;
UINT32 d;
UINT32 k;
UINT32 x;
UINT64 pD = 0;
UINT64 pJ = 0;
UINT64 pK = 0;
for(k = START_N; !haveFound; k++)
{
for(j = k - 1; j >= START_N; j--)
{
pK = CalcPentagonNum(k);
pJ = CalcPentagonNum(j);
if(IsPentagonNum(pK - pJ, d) && IsPentagonNum(pK + pJ, x))
{
haveFound = true;
pD = pK - pJ;
break;
}
}
}
cout << "j = " << j << endl
<< "pJ = " << pJ << endl
<< "d = " << d << endl
<< "pD = " << pD << endl
<< "k = " << k << endl
<< "pK = pD + pJ = " << pK << endl
<< "x = " << x << endl
<< "pX = pK + pJ = " << pK+pJ << endl;
/*********************************算法结束*******************************/
QueryPerformanceCounter(&timeEnd);
cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;
time_t currentTime = time(NULL);
const char BEEP_CHAR = '\007';
const int MAX_TIME_CHAR = 30;
char timeStr[MAX_TIME_CHAR];
ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime);
cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR;
system("pause");
}
//主函数
int main()
{
F1();
return 0;
}
/*
void F1()
j = 1020
pJ = 1560090
d = 1912
pD = 5482660
k = 2168
pK = pD + pJ = 7042750
x = 2395
pX = pK + pJ = 8602840
Total Milliseconds is 432.123
By GodMoon
Mon Mar 11 20:13:38 2013
*/


 

这篇关于【ProjectEuler】ProjectEuler_044的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/780130

相关文章

UnityShader源码2017---学习笔记与自我拓展044

源自 Particle Anim Alpha Blend   这是一个伟大的shader。这里不讲原理,请移步这里,这里,这里,和这里。   假设已经看懂了上面的那4篇。 我们看一下这个shader的用法。 struct appdata_t {float4 vertex : POSITION;fixed4 color : COLOR;float4 texcoords : TEXCOOR

Leetcode 044 Wildcard Matching(DP)

题目连接:Leetcode 044 Wildcard Matching 解题思路:动态规划,dp[i][j]表示s[0]~s[i-1]和p[0]~p[j-1]是否可以匹配。转移方程有:     s[i] == p[j] 时:dp[i][j] |= dp[i-1][j-1]     p[j] == '*'时:dp[i][j] |= dp[i-1][j] | dp[i][j-1]   (分别对应

【JavaScript 漫游】【044】Web Worker

文章简介 本篇文章为【JavaScript 漫游】专栏的第 044 篇文章,对浏览器模型的 Web Worker 相关知识点进行了总结。 概述 JavaScript 语言采用的是单线程模型,也就是说,所有任务只能在一个线程上完成,一次只能做一件事。前面的任务没做完,后面的任务只能等着。随着电脑计算能力的增强,尤其是多核 CPU 的出现,单线程带来很大的不便,无法充分发挥计算机的计算能力。

044基于深度学习的鱼类检测

demo仓库和视频演示找044期: 到此一游7758258的个人空间_哔哩哔哩_bilibili 效果展示图如下:  代码文件展示如下: 运行01数据集文本生成制作.py可以读取图片路径保存再txt文本中, 运行02train.py可以对txt文本中的图片路径读取并训练模型, 运行03pyqt界面.py可以生成一个可视化的界面,通过点击按钮加载图片识别。

【ProjectEuler】ProjectEuler_052(找出最小的正整数x,使得2x, 3x, 4x, 5x和6x都包含同样的数字)

#pragma once#include <windows.h>#include <vector>#include <set>using namespace std;class MoonMath{public:MoonMath(void);~MoonMath(void);//************************************// Method: IsInt//

【ProjectEuler】ProjectEuler_051(找出最小的能够通过改变同一部分得到八个质数的质数)

#pragma once#include <windows.h>#include <vector>#include <set>using namespace std;class MoonMath{public:MoonMath(void);~MoonMath(void);//************************************// Method: IsInt//

【ProjectEuler】ProjectEuler_050(找到100万以内最多连续素数的和,它也同时是个素数)

#pragma once#include <windows.h>#include <vector>#include <set>using namespace std;class MoonMath{public:MoonMath(void);~MoonMath(void);//************************************// Method: IsInt//

【ProjectEuler】ProjectEuler_049

#pragma once#include <windows.h>#include <vector>#include <set>using namespace std;class MoonMath{public:MoonMath(void);~MoonMath(void);//************************************// Method: IsInt//

【ProjectEuler】ProjectEuler_047

#pragma once#include <windows.h>#include <vector>#include <set>using namespace std;class MoonMath{public:MoonMath(void);~MoonMath(void);//************************************// Method: IsInt//

【ProjectEuler】ProjectEuler_045

#pragma once#include <windows.h>class MoonMath{public:MoonMath(void);~MoonMath(void);//************************************// Method: IsInt// Access: public // Describe: 判断double值在eps