【ProjectEuler】ProjectEuler_045

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

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

#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);
};


 

#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 = 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;
}


 

// Triangular, pentagonal, and hexagonal
//     Problem 45
//     Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
//
// Triangle	 	Tn=n(n+1)/2	 	1, 3, 6, 10, 15, ...
//     Pentagonal	 	Pn=n(3n-1)/2	 	1, 5, 12, 22, 35, ...
//     Hexagonal	 	Hn=n(2n-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.
#include <iostream>
#include <windows.h>
#include <ctime>
#include <assert.h>
#include <string>
#include <MoonMath.h>
using namespace std;
//************************************
// Method:    CalcPentagonNum
// Access:    public
// Describe:  计算第n个Triangle数
// Parameter: UINT32 n
// Returns:   UINT32
//************************************
inline UINT64 CalcTriangleNum(UINT32 n)
{
return UINT64(n) * (n + 1) / 2;
}
//************************************
// Method:    CalcHexagonalNum
// Access:    public
// Describe:  计算第n个Hexagonal数
// Parameter: UINT32 n
// Returns:   UINT32
//************************************
inline UINT64 CalcHexagonalNum(UINT32 n)
{
return UINT64(n) * (2 * n - 1);
}
//************************************
// 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:    IsTriangleNum
// Access:    public
// Describe:  判断num是否是Triangle数
// Parameter: UINT32 num    要判断的Triangle数
// Parameter: UINT32 &n     如果是Triangle数,返回n,否则返回无效值
// Returns:   bool          是Triangle数返回true,否则返回false
//************************************
bool IsTriangleNum(UINT64 num, UINT32 &n)
{
// 根据一元二次方程求解公式求出n的值
// n^2+n-2*num=0
// n=(-1+-sqrt(1+8*num))/2
// 因为n>0
// 所以n=(-1+sqrt(1+8*num))/2
double nDouble = (-1.0 + sqrt(1.0 + 8.0 * num)) / 2;
const double EPSILON = 0.00001; // 判断n是否为整数的精确度
n = 0;
// 如果n是整数,则视计算Pentagon的公式可逆推,num为Pentagon数
if(!MoonMath::IsInt(nDouble, EPSILON, (INT32 &)n))
{
return false;
}
return CalcTriangleNum(n) == num;
}
//************************************
// Method:    IsHexagonalNum
// Access:    public
// Describe:  判断num是否是Hexagonal数
// Parameter: UINT32 num    要判断的Hexagonal数
// Parameter: UINT32 &n     如果是Hexagonal数,返回n,否则返回无效值
// Returns:   bool          是Hexagonal数返回true,否则返回false
//************************************
bool IsHexagonalNum(UINT64 num, UINT32 &n)
{
// 根据一元二次方程求解公式求出n的值
// 2*n^2-n-num=0
// n=(1+-sqrt(1+8*num))/4
// 因为n>0
// 所以n=(1+sqrt(1+8*num))/4
double nDouble = (1.0 + sqrt(1.0 + 8.0 * num)) / 4;
const double EPSILON = 0.00001; // 判断n是否为整数的精确度
n = 0;
// 如果n是整数,则视计算Pentagon的公式可逆推,num为Pentagon数
if(!MoonMath::IsInt(nDouble, EPSILON, (INT32 &)n))
{
return false;
}
return CalcHexagonalNum(n) == num;
}
//************************************
// 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 = CalcTriangleNum(i);
if(!IsTriangleNum(pI, n))
{
cout << "i = " << i << endl << "CalcTriangleNum(i) = " << i << endl;
}
pI = CalcHexagonalNum(i);
if(!IsHexagonalNum(pI, n))
{
cout << "i = " << i << endl << "CalcHexagonalNum(i) = " << i << endl;
}
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);
/*********************************算法开始*******************************/
UINT32 pI = 0;
UINT32 hexN;
UINT32 penN;
const UINT32 MIN_FIT_NUM = 40755;
UINT32 i;
for(IsTriangleNum(MIN_FIT_NUM, i), ++i;; ++i)
{
pI = CalcTriangleNum(i);
if(IsHexagonalNum(pI, hexN) && IsPentagonNum(pI, penN))
{
break;
}
}
cout << pI << 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()
1533776805
Total Milliseconds is 16.5489
By GodMoon
Sun Mar 10 21:55:49 2013
*/


 

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



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

相关文章

新160个crackme - 045-CyTom-crackme

运行分析 需要破解Name和Serial PE分析 Delphi程序,32位,无壳 静态分析&动态调试 ida找到关键字符串,双击进入 动调主函数,函数sub_4251A0作用未知 函数sub_4251A0作用:Name每个字符ascii相乘 算法分析 Name = 'concealbear'a1 = 1for i in range(le

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

源自Internal-BlitCopy,Internal-BlitCopyDepth,Internal-CombineDepthNormals 讲一下unity的潜规则 Shader "Hidden/Internal-CombineDepthNormals" {} 只有以Hidden/开头的shader,都会在shader列表中隐藏起来。   BlitCopy从名字上看应该是Blit()

Leetcode 045 Jump Game II(DP)

题目连接:Leetcode 045 Jump Game II 解题思路:动态规划,dp[i]表示到第i个位置最少需要几步。用一个优先队列维护在第i个位置之前最小的dp[k]值,每次取出一个最小的k,判断k+num[k]是否大于等于i,如果大于等于,那么dp[i] = dp[k] + 1;否则删除k,并再取出优先队列的头,直到dp[i]被更新,然后将dp[i]也放入优先队列中。 class So

pta L1-045 宇宙无敌大招呼

L1-045 宇宙无敌大招呼 分数 5 全屏浏览 切换布局 作者 陈越 单位 浙江大学 据说所有程序员学习的第一个程序都是在屏幕上输出一句“Hello World”,跟这个世界打个招呼。作为天梯赛中的程序员,你写的程序得高级一点,要能跟任意指定的星球打招呼。 输入格式: 输入在第一行给出一个星球的名字S,是一个由不超过7个英文字母组成的单词,以回车结束。 输出格式: 在

【QT+QGIS跨平台编译】045:【netcdf3+Qt跨平台编译】(一套代码、一套框架,跨平台编译)

点击查看专栏目录 文章目录 一、NetCDF3介绍二、文件下载三、文件分析四、pro文件五、编译实践 一、NetCDF3介绍   NetCDF(Network Common Data Form)是一种用于存储科学数据的文件格式和库。NetCDF3 是 NetCDF 的旧版本,通常指的是 NetCDF 版本 3.x。   以下是 NetCDF3 的一些特点和介绍: 数据

S2-052远程代码执行漏洞和S2-045远程代码执行漏洞复现

1.复现S2-052远程代码执行漏洞 (1) cd vulhub/struts2/s2-052 切换目录。 (2) docker-compose up -d 启动 (3) http://192.168.80.157:8080/orders.xhtml (4)burpsuite抓包。 (5)send to repeater,修改数据包: POST /orders/3/

【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//