开发者

New answer same number? C++

So I have this program:

#include <iostream>

using 开发者_如何学Cnamespace std;


bool prime(int input)
{
//    cout << "pinput: " << input << endl;
    int i = ((input/2) + 1);
  //      cout << "pi: " << i << endl;
    int c;
    for (i>0; i--;){
        //cout << "pi: " << i << endl;
        if (input == 3 || input == 2){
         //   cout << "true" << endl;
            return true;
        }
        if (input == 1){
       //     cout << "pi = 1" << endl;
            return false;
        }
    c= input%i;
    if (c==0 || i == 1 ){
     //   cout << "false" << endl;
        return false;
    }
    else if (c!=0 && i<4){
   //     cout << "true" << endl;
        return true;
    }
    }
return 0;
}

int factor(int input){
//    cout << "finput: " << input << endl;
   int i = (input/2) + 1;
    int c;
    int e;
    bool d = false;
    for (i>0; i--;){
  // cout << "fi: " << i << endl;
        c = input%i;
        if (c==0){
        d = prime(i);
        if (d==true){
    //    cout << "found" << endl;
        return i;}
        }
        if (i==1){
  //          cout << "fi = 1";
            return 0;
        }
//cout << "not prime" << endl;
        }
    return 0;
}

int main(){
    int woot;
    cout << "Please insert quater: " <<endl;
    cin >> woot;
    int answer;
    answer = factor(woot);
    if (answer == 0)
    cout << "no prime factors" << endl;
    else
    cout << "answer is: " <<answer << endl;

return 0;
}

It seems to work until I put a really big number in like more specifically the number 600851475143, in which case I always get different answers when I run that number now I'm pretty sure it's just exceeding the size of it's variable type. Now then I was looking and I can't find the right variable type for a number that big, I int and long seem to be for numbers that are for numbers up to 4294967295 if unsigned however that is only 10 digits long, mine is 12. What type of variable should I use? Or will that even fix the problem? The program is to find the largest prime factor of a number (Euler problem 3). Any tips links or advice would be appreciated. And of course an answer extra appreciated! :D


Interesting typo alert!

This is unlikely to be doing what you think it is doing...

for (i>0; i--;){

While it is perfectly legal syntax, and will loop the correct number of times, the value of i inside the loop is (probably) going to be one less than you intended...

% cat 4237157.c++ 
#include <iostream>

int main() 
{
    {
        std::cout << "Your loop: " << std::endl;
        int i = 10;
        for (i>0; i--;) 
        {
            std::cout << i << std::endl;
        }
    }
    {
        std::cout << "More conventionally: " << std::endl;
        for (int i = 10; i > 0; i--) 
        {
            std::cout << i << std::endl;
        }
    }
    return EXIT_SUCCESS;
}

% g++ -o 4237157{,.c++}

% ./4237157 
Your loop: 
9
8
7
6
5
4
3
2
1
0
More conventionally: 
10
9
8
7
6
5
4
3
2
1

The syntax for a for-loop in C-like languages is:

for (variable initialization; conditional; variable increment)

You are evaluating "i>0" instead of doing any initalization. This may as well be blank. Then you are evaluating whether i-- is zero. Since i is post-decremented, your loop starts with i being one less than it was initialized with before the loop, executes until (and including) being equal to zero and then terminates.


A lot of the problems on Project Euler call for arbitrary-precision arithmetic, which isn't covered by the C++ standard library.

Have a look at the C++ Big Integer Library.


If you want arbitarily big numbers, you need an arbitary precision arithmetic library



unsigned long                 4294967295
unsigned long long  18446744073709551615


unsigned long long is not standard C++, but most compilers support it as an extension. The maximum should be at least 2^64 - 1, which is more than enough.

If you later want even larger numbers, you can use a arbitrary precision library such as GMP. They have a C++ interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜