“整数常量对于‘long’来说太大了;类型”当找到最大素因数时

发布于 2024-10-14 03:52:20 字数 1709 浏览 7 评论 0原文

我正在致力于解决 Euler 项目 3:

Description: The prime factors of 13195 are 5, 7, 13 and 29.
             What is the largest prime factor of the number 600851475143 ?

这是我生成答案的代码。但是我需要一个整数类型来保存600851475143。当我在 Mac 上的 GCC 上编译它时,我得到:

integer constant is too large for ‘long’ type". 

我预计 long long 可以轻松保存这个数字。我也尝试将其设为未签名。为什么我的代码不能保存那么小的数字,我该怎么做才能使它工作?

#include <iostream>
#include <vector>

using namespace std;

static bool IsPrimeFactor(int numToTest,int testNum,vector<int> factors)
{
    if(numToTest%testNum==0) // is it a factor?
    {
        // see if it is a prime factor
        for(unsigned int i=0; i < factors.size(); i++)
        {
            if(testNum%factors[i]==0)  // see if this factor
            {                          //is divisble by one we have already

                return false;
            }
        }

        return true;
    }
    return false;
}

int main() {
    unsigned long long numToTest=600851475143;
    unsigned int testNum=2;  // 0 and 1 are assumed
    vector<int> factors;   

    while(testNum<numToTest)   // don't go higher than the max num
    {
        if(IsPrimeFactor(numToTest,testNum,factors)) // is it a factor?
        {
            factors.push_back(testNum); // got through prime factors 
        }                                   // and none divided it

        testNum++;
    }

    for(unsigned int i=0; i < factors.size(); i++)
    {
        cout << "factor " <<factors[i] << endl;
    }

    cout<<"Highest factor: " << factors[factors.size()-1]<<endl;

    return 0;
}

I am working on solving Euler project 3:

Description: The prime factors of 13195 are 5, 7, 13 and 29.
             What is the largest prime factor of the number 600851475143 ?

This is my code to generate the answer. However I need an integer type to hold 600851475143. When I compile this on GCC on a Mac I get:

integer constant is too large for ‘long’ type". 

I expect long long could easily hold this number. I also tried making it unsigned. Why doesn't my code hold that small number and what can I do to make it work?

#include <iostream>
#include <vector>

using namespace std;

static bool IsPrimeFactor(int numToTest,int testNum,vector<int> factors)
{
    if(numToTest%testNum==0) // is it a factor?
    {
        // see if it is a prime factor
        for(unsigned int i=0; i < factors.size(); i++)
        {
            if(testNum%factors[i]==0)  // see if this factor
            {                          //is divisble by one we have already

                return false;
            }
        }

        return true;
    }
    return false;
}

int main() {
    unsigned long long numToTest=600851475143;
    unsigned int testNum=2;  // 0 and 1 are assumed
    vector<int> factors;   

    while(testNum<numToTest)   // don't go higher than the max num
    {
        if(IsPrimeFactor(numToTest,testNum,factors)) // is it a factor?
        {
            factors.push_back(testNum); // got through prime factors 
        }                                   // and none divided it

        testNum++;
    }

    for(unsigned int i=0; i < factors.size(); i++)
    {
        cout << "factor " <<factors[i] << endl;
    }

    cout<<"Highest factor: " << factors[factors.size()-1]<<endl;

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2024-10-21 03:52:20

检查这个问题。你必须像这样指定你的文字:

600851475143LL

Check this question. You have to specify your literal like this:

600851475143LL
对你而言 2024-10-21 03:52:20

正如@Space_C0wb0y所说,您需要为文字指定一个后缀。

另外,您的 IsPrimeFactor 函数将会遇到问题 - 参数是整数,但正如您已经发现的那样,整数甚至长整型都不足以存储您的数字会反复经过...

As @Space_C0wb0y said, you need to specify a suffix for the literal.

Also, you're going to have a problem with your IsPrimeFactor function - the parameters are ints, but as you've already discovered, an int or even a long is not big enough to store the number you'll be passing in repeatedly...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文