How to make an integer store a 12-digit number?

Asked

Viewed 3,092 times

1

I need to find the largest prime factor of the number 600851475143.

Because it is a very large number, I cannot store it in a normal integer type variable.

The code I made to find the largest prime factor of a number is this:

#include <iostream>

using namespace std;

int main()
{
    int maiorFator = 0;
    unsigned long int numero = 600851475143;

    for (int i = 2; numero != 1;) {
        if (numero % i == 0) {
            numero /= i;
            if (maiorFator < i) {
                maiorFator = i;
            }
            cout << i << endl;
        }
        else {
            i++;
        }
    }
    cout << "\nMaior fator primo: " << maiorFator << endl;

    return 0;
}

I’ve tried using the modifiers long and unsigned in the variable numero, but it doesn’t work, it always occurs overflow, and because of that I can’t get the result.

I can’t use variables like float nor double, because I use the rest operator % to make the calculations.

The factorization algorithm works well, at least for the numbers I tested. My only problem is not being able to store the large number.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

3

I will answer only what was asked.

The correct type in this case is the long long that guarantees in specification the 64 bits required for a number of this size, other types may have this size if the compiler wants, but does not guarantee.

That said, the algorithm is wrong but I think you’ll want to fix it yourself since it’s an exercise.

0

you can use one of these options. the first if you want a variable that stores only whole the second if you want something more global

long long; long double

you can also choose to create a class with this type of prototyped variable, and add it to a repository/library of your own

long long uVar(long long myUVar) {return myUVar;}

alternatively

long double uVar(long long myUVar) {return myUVar;}

-1

a possible approach, will be the technique of serialization, widely used to record states in video game graphics engines, especially if we take into account the beginnings, and that can be used for gpgpu purposes



http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/tutorial.html





#include <fstream>

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

/////////////////////////////////////////////////////////////
// gps coordinate
//
// illustrates serialization for a simple type
//
class gps_position
{
private:
    friend class boost::serialization::access;
    // When the class Archive corresponds to an output archive, the
    // & operator is defined similar to <<.  Likewise, when the class Archive
    // is a type of input archive the & operator is defined similar to >>.
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & degrees;
        ar & minutes;
        ar & seconds;
    }
    int degrees;
    int minutes;
    float seconds;
public:
    gps_position(){};
    gps_position(int d, int m, float s) :
        degrees(d), minutes(m), seconds(s)
    {}
};

int main() {
    // create and open a character archive for output
    std::ofstream ofs("filename");

    // create class instance
    const gps_position g(35, 59, 24.567f);

    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << g;
        // archive and stream closed when destructors are called
    }

    // ... some time later restore the class instance to its orginal state
    gps_position newg;
    {
        // create and open an archive for input
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> newg;
        // archive and stream closed when destructors are called
    }
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.