large integers

T

Tim S Roberts

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
 
V

Victor Bazarov

Tim said:
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?)
 
T

Tim S Roberts

Victor Bazarov said:
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
 
M

Martin Steen

Tim said:
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
 
P

Puppet_Sock

Tim said:
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
 
V

Victor Bazarov

Tim said:
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
 
C

Clark S. Cox III

Martin said:
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
 
J

jjds101

Clark said:
Martin said:
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.
 
J

jjds101

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!
 
M

Martin Steen

Clark said:
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
 
M

Martin Steen

Clark said:
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
 
M

Martin Steen

Clark said:
> 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top