large integers

Discussion in 'C++' started by Tim S Roberts, Oct 12, 2006.

  1. If I have a value, say 13849, I can assign it to x and test if it divisible
    by 37 by something like
    if (x%37==0)...
    and I can test if it's a square by something like
    int y;
    y=sqrt(x);
    if (x==y*y)...

    But my question is, how can I do these two things (preferably as simply and
    efficiently as possible) if my value is not 13849, but, say, 176457628349?

    Tim
     
    Tim S Roberts, Oct 12, 2006
    #1
    1. Advertising

  2. Tim S Roberts wrote:
    > If I have a value, say 13849, I can assign it to x and test if it
    > divisible by 37 by something like
    > if (x%37==0)...
    > and I can test if it's a square by something like
    > int y;
    > y=sqrt(x);
    > if (x==y*y)...
    >
    > But my question is, how can I do these two things (preferably as
    > simply and efficiently as possible) if my value is not 13849, but,
    > say, 176457628349?


    If your compiler supports numbers with the range that includes your
    176457628349, use that type. You would have to ask in the newsgroup
    for your compiler about it, or read the manual (oops, did I just write
    that?)

    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 12, 2006
    #2
    1. Advertising

  3. "Victor Bazarov" <> wrote in message
    news:eglkaj$6fb$...
    > Tim S Roberts wrote:
    >> If I have a value, say 13849, I can assign it to x and test if it
    >> divisible by 37 by something like
    >> if (x%37==0)...
    >> and I can test if it's a square by something like
    >> int y;
    >> y=sqrt(x);
    >> if (x==y*y)...
    >>
    >> But my question is, how can I do these two things (preferably as
    >> simply and efficiently as possible) if my value is not 13849, but,
    >> say, 176457628349?

    >
    > If your compiler supports numbers with the range that includes your
    > 176457628349, use that type. You would have to ask in the newsgroup
    > for your compiler about it, or read the manual (oops, did I just write
    > that?)


    Thanks for this - but I don't think I have such a type available (I'm using
    Microsoft Visual C++ 6.0). Are there any workarounds I can use? Or will I
    have to use Mathematica, or something (the problem is, I'm using very large
    numbers, *and* I need speed....)

    Tim
     
    Tim S Roberts, Oct 12, 2006
    #3
  4. Tim S Roberts

    Martin Steen Guest

    Tim S Roberts wrote:
    > If I have a value, say 13849, I can assign it to x and test if it divisible
    > by 37 by something like
    > if (x%37==0)...
    > and I can test if it's a square by something like
    > int y;
    > y=sqrt(x);
    > if (x==y*y)...
    >
    > But my question is, how can I do these two things (preferably as simply and
    > efficiently as possible) if my value is not 13849, but, say, 176457628349?
    >
    > Tim
    >


    Some compilers (e.g. g++) support the __int64 datatype.

    __int64 BigIntVar = (__int64) 176457628349LL;
    cout << (BigIntVar % 37) << endl;

    - Martin
     
    Martin Steen, Oct 12, 2006
    #4
  5. Tim S Roberts

    Puppet_Sock Guest

    Tim S Roberts wrote:
    > If I have a value, say 13849, I can assign it to x and test if it divisible
    > by 37 by something like
    > if (x%37==0)...
    > and I can test if it's a square by something like
    > int y;
    > y=sqrt(x);
    > if (x==y*y)...
    >
    > But my question is, how can I do these two things (preferably as simply and
    > efficiently as possible) if my value is not 13849, but, say, 176457628349?


    There are two questions here:

    The first is, how to efficiently do things like factoring, checking
    if one integer factors another, checking for perfect square, etc.
    For this you probably want to visit some math groups, or possibly
    a group that has the word numerical or the word algorithm or
    such in its name. Or possibly do some searches at
    groups.google.com.

    The other question is, how to represent very large integers in C++.
    There are various libraries that allow extended precision, or precision
    of arbitary degree. You need to be prepared for the fact that they
    are going to get slower the longer the integer. Again, google is your
    friend here. Search for the FAQ in this news group, and from the
    FAQ look for libraries available. Also google for C++ arbitrary
    precision libraries.
    Socks
     
    Puppet_Sock, Oct 12, 2006
    #5
  6. Tim S Roberts wrote:
    > "Victor Bazarov" <> wrote in message
    > news:eglkaj$6fb$...
    >> Tim S Roberts wrote:
    >>> If I have a value, say 13849, I can assign it to x and test if it
    >>> divisible by 37 by something like
    >>> if (x%37==0)...
    >>> and I can test if it's a square by something like
    >>> int y;
    >>> y=sqrt(x);
    >>> if (x==y*y)...
    >>>
    >>> But my question is, how can I do these two things (preferably as
    >>> simply and efficiently as possible) if my value is not 13849, but,
    >>> say, 176457628349?

    >>
    >> If your compiler supports numbers with the range that includes your
    >> 176457628349, use that type. You would have to ask in the newsgroup
    >> for your compiler about it, or read the manual (oops, did I just
    >> write that?)

    >
    > Thanks for this - but I don't think I have such a type available (I'm
    > using Microsoft Visual C++ 6.0).


    Asking in 'microsoft.public.vc.language' might help. Hint: __int64.

    > Are there any workarounds I can
    > use? Or will I have to use Mathematica, or something (the problem
    > is, I'm using very large numbers, *and* I need speed....)


    Try looking on the Web for "arbitrary precision library" or "very large
    integer library" or some such.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 12, 2006
    #6
  7. Martin Steen wrote:
    > Tim S Roberts wrote:
    >> If I have a value, say 13849, I can assign it to x and test if it
    >> divisible by 37 by something like
    >> if (x%37==0)...
    >> and I can test if it's a square by something like
    >> int y;
    >> y=sqrt(x);
    >> if (x==y*y)...
    >>
    >> But my question is, how can I do these two things (preferably as
    >> simply and efficiently as possible) if my value is not 13849, but,
    >> say, 176457628349?
    >>
    >> Tim
    >>

    >
    > Some compilers (e.g. g++) support the __int64 datatype.
    >
    > __int64 BigIntVar = (__int64) 176457628349LL;
    > cout << (BigIntVar % 37) << endl;
    >
    > - Martin
    >


    You sure about g++ supporting __int64?

    [ccox-macbook:~] ccox% cat test.cpp
    #include <iostream>
    using namespace std;

    int main()
    {
    __int64 BigIntVar = (__int64) 176457628349LL;
    cout << (BigIntVar % 37) << endl;
    return 0;
    }

    [ccox-macbook:~] ccox% g++ test.cpp
    test.cpp: In function ‘int main()’:
    test.cpp:6: error: ‘__int64’ was not declared in this scope
    test.cpp:6: error: expected `;' before ‘BigIntVar’
    test.cpp:7: error: ‘BigIntVar’ was not declared in this scope



    --
    Clark S. Cox III
     
    Clark S. Cox III, Oct 12, 2006
    #7
  8. Tim S Roberts

    Guest

    Clark S. Cox III wrote:
    > Martin Steen wrote:
    > > Tim S Roberts wrote:
    > >> If I have a value, say 13849, I can assign it to x and test if it
    > >> divisible by 37 by something like
    > >> if (x%37==0)...
    > >> and I can test if it's a square by something like
    > >> int y;
    > >> y=sqrt(x);
    > >> if (x==y*y)...
    > >>
    > >> But my question is, how can I do these two things (preferably as
    > >> simply and efficiently as possible) if my value is not 13849, but,
    > >> say, 176457628349?
    > >>
    > >> Tim
    > >>

    > >
    > > Some compilers (e.g. g++) support the __int64 datatype.
    > >
    > > __int64 BigIntVar = (__int64) 176457628349LL;
    > > cout << (BigIntVar % 37) << endl;
    > >
    > > - Martin
    > >

    >
    > You sure about g++ supporting __int64?
    >
    > [ccox-macbook:~] ccox% cat test.cpp
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > __int64 BigIntVar = (__int64) 176457628349LL;
    > cout << (BigIntVar % 37) << endl;
    > return 0;
    > }
    >
    > [ccox-macbook:~] ccox% g++ test.cpp
    > test.cpp: In function 'int main()':
    > test.cpp:6: error: '__int64' was not declared in this scope
    > test.cpp:6: error: expected `;' before 'BigIntVar'
    > test.cpp:7: error: 'BigIntVar' was not declared in this scope
    >


    I got "long long" to work with g++ on solaris.

    #include <iostream>
    #include <limits>

    int main () {
    unsigned long long x = 55555555555555LL;
    std::cout << "long long max: "
    << std::numeric_limits<long long>::max() << '\n'
    << "size of long long: "
    << sizeof(x) << '\n';

    return 0;

    }

    long long max: 9223372036854775807
    size of long long: 8

    Note that you need the LL at the end of the constant or it doesn't work.
     
    , Oct 12, 2006
    #8
  9. Tim S Roberts

    Guest

    >
    > I got "long long" to work with g++ on solaris.
    >
    > #include <iostream>
    > #include <limits>
    >
    > int main () {
    > unsigned long long x = 55555555555555LL;
    > std::cout << "long long max: "
    > << std::numeric_limits<long long>::max() << '\n'
    > << "size of long long: "
    > << sizeof(x) << '\n';
    >
    > return 0;
    >
    > }
    >
    > long long max: 9223372036854775807
    > size of long long: 8
    >
    > Note that you need the LL at the end of the constant or it doesn't work.


    Oops --- sorry, shouldn't have had the "unsigned" there!
     
    , Oct 12, 2006
    #9
  10. Tim S Roberts

    Martin Steen Guest

    Clark S. Cox III wrote:

    >
    > You sure about g++ supporting __int64?
    >
    > [ccox-macbook:~] ccox% cat test.cpp
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > __int64 BigIntVar = (__int64) 176457628349LL;
    > cout << (BigIntVar % 37) << endl;
    > return 0;
    > }
    >
    > [ccox-macbook:~] ccox% g++ test.cpp
    > test.cpp: In function ‘int main()’:
    > test.cpp:6: error: ‘__int64’ was not declared in this scope
    > test.cpp:6: error: expected `;' before ‘BigIntVar’
    > test.cpp:7: error: ‘BigIntVar’ was not declared in this scope
    >


    Ok, you're right. I am using the mingw-version
    of g++, which is different.

    If you replace "__int64" by "long long", the
    code should work (it works on debian linux with
    a "normal" g++ compiler).

    - Martin
     
    Martin Steen, Oct 12, 2006
    #10
  11. Tim S Roberts

    Martin Steen Guest

    Clark S. Cox III wrote:

    > You sure about g++ supporting __int64?
    >


    I tested the code on a "mingw"-version of g++.
    g++ --version: g++.exe (GCC) 3.4.2 (mingw-special)

    Other versions of g++ seem to use "long long" instead
    (testet on debian linux).

    - Martin
     
    Martin Steen, Oct 12, 2006
    #11
  12. Tim S Roberts

    Martin Steen Guest

    Clark S. Cox III wrote:

    > You sure about g++ supporting __int64?
    >


    I tested the code on a "mingw"-version of g++.
    g++ --version: g++.exe (GCC) 3.4.2 (mingw-special)

    Other versions of g++ seem to use "long long" instead
    (testet on debian linux).

    - Martin
     
    Martin Steen, Oct 12, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    331
    Ryan Stewart
    Jan 20, 2005
  2. Michael Zielinski

    Large integers

    Michael Zielinski, Dec 1, 2004, in forum: C++
    Replies:
    6
    Views:
    422
    Alex Vinokur
    Dec 20, 2004
  3. Replies:
    0
    Views:
    299
  4. very large integers

    , Oct 8, 2006, in forum: C++
    Replies:
    13
    Views:
    602
    Ivan Vecerina
    Oct 9, 2006
  5. Replies:
    7
    Views:
    360
    Jorgen Grahn
    Apr 1, 2009
Loading...

Share This Page