【ProjectEuler】ProjectEuler_047

#pragma once

#include <windows.h>
#include <vector>
#include <set>

using namespace std;

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

    //************************************
    // Method:    GetDiffPrimerFactorNum
    // Access:    public static
    // Describe:  获取num所有的不同质因数
    // Parameter: UINT32 num
    // Returns:   set<UINT32>
    //************************************
    static set<UINT32> MoonMath::GetDiffPrimerFactorNum(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;
}

set<UINT32> MoonMath::GetDiffPrimerFactorNum(UINT32 num)
{
    UINT32 halfNum = num / 2;
    set<UINT32> factors;

    for(UINT32 i = 2; i <= halfNum; ++i)
    {
        if(!MoonMath::IsPrimer(i))
        {
            continue;
        }

        if(num % i == 0)
        {
            factors.insert(i);

            while(num % i == 0)
            {
                num /= i;
            }
        }
    }

    return factors;
}

// Distinct primes factors
//     Problem 47
//     The first two consecutive numbers to have two distinct prime factors are:
//
//     14 = 2 * 7
//     15 = 3 * 5
//
//     The first three consecutive numbers to have three distinct prime factors are:
//
//     644 = 2^2 * 7 * 23
//     645 = 3 * 5 * 43
//     646 = 2 * 17 * 19.
//
//     Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers?

#include <iostream>
#include <windows.h>
#include <ctime>
#include <assert.h>

#include <MoonMath.h>
using namespace std;

// 打印时间等相关信息
class DetailPrinter
{
public:
    void Start();
    void End();
    DetailPrinter();

private:
    LARGE_INTEGER timeStart;
    LARGE_INTEGER timeEnd;
    LARGE_INTEGER freq;
};

DetailPrinter::DetailPrinter()
{
    QueryPerformanceFrequency(&freq);
}

//************************************
// Method:    Start
// Access:    public
// Describe:  执行每个方法前调用
// Returns:   void
//************************************
void DetailPrinter::Start()
{
    QueryPerformanceCounter(&timeStart);
}

//************************************
// Method:    End
// Access:    public
// Describe:  执行每个方法后调用
// Returns:   void
//************************************
void DetailPrinter::End()
{
    QueryPerformanceCounter(&timeEnd);
    cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;

    const char BEEP_CHAR = '\007';

    cout << endl << "By GodMoon" << endl << __TIMESTAMP__ << BEEP_CHAR << endl;

    system("pause");
}

/*************************解题开始*********************************/

void TestFun1()
{

    cout << "Test OK!" << endl;
}

void F1()
{
    cout << "void F1()" << endl;

    // TestFun1();

    DetailPrinter detailPrinter;
    detailPrinter.Start();

    /*********************************算法开始*******************************/
    const UINT32 NUM_COUNT = 4;
    const UINT32 BEGIN_NUM = 1;

    UINT32 continueCount = 0;
    bool haveFound = false;
    UINT32 num;

    for(num = BEGIN_NUM; !haveFound; ++num)
    {
        if(MoonMath::GetDiffPrimerFactorNum(num).size() == NUM_COUNT)
        {
            ++continueCount;

            if(continueCount >= NUM_COUNT)
            {
                haveFound = true;
                break;
            }

            continue;
        }

        continueCount = 0;
    }

    if(haveFound)
    {
        cout << "The first num which have " << NUM_COUNT << " distinct prime factors is " << num - NUM_COUNT + 1 << endl;
    }
    else
    {
        cout << "Not Found!" << endl;
    }

    /*********************************算法结束*******************************/

    detailPrinter.End();
}

//主函数
int main()
{
    F1();
    return 0;
}

/*
void F1()
The first num which have 4 distinct prime factors is 134043
Total Milliseconds is 679360

By GodMoon
Mon Mar 11 21:00:03 2013
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值