hello. Ive been using a pretty long number in my program . its problem3 of the euler project . Its 600851475143 . What data type do I use for this???

Recommended Answers

All 23 Replies

.

a "long long" data type is a 64 bit number, and gives

Signed min: -9223372036854775808
Signed max: 9223372036854775807
Unsigned max: 18446744073709551615


.

a "long long" data type is a 64 bit number, and gives

Signed min: -9223372036854775808
Signed max: 9223372036854775807
Unsigned max: 18446744073709551615


.

Thank you>>>And what is the format spec?Im working on devC++

the format specifier for a long long int is

long long myBigNum=600851475143;

printf("the number is:  %lld\n",myBigNum);

.

the format specifier for a long long int is

long long myBigNum=600851475143;

printf("the number is:  %lld\n",myBigNum);

.

Thank you

If you want to use numbers of very huge sizes i bet strings might be useful. You must perhaps use several functions for performing those operations. for eg. when you add two strings, proceed from the last digit and carry over the excess to the next digit and so on.

commented: ridiculous -2

[IMG]http://img8.imageshack.us/my.php?image=devsnap.png[/IMG]
Im still not able to get the precision required

i dont know why not.

i'm using GCC v3.4, which is over 3 years old.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    unsigned long long bignum = 600851475143 ;

    printf("bignum    : %lld\n",bignum);
    printf("bignum * 2: %lld\n",bignum * 2);
    printf("bignum * 4: %lld\n",bignum * 4);
    return 0;
}

gives the following result:

bignum    : 600851475143
bignum * 2: 1201702950286
bignum * 4: 2403405900572

Process returned 0 (0x0)   execution time : 0.050 s
Press any key to continue.

you're either doing something wrong, or your compiler sucks or something.

Can you tell me, why is it wrong?? You can very well use atoi function. for converting characters to numbers

>Jepthah Without knowing how it works, it is bad to comment. Well you know operator overloading???? and you can very well stop being rude.

Can you tell me, why is it wrong?? You can very well use atoi function. for converting characters to numbers

beat it, kid. your answer isn't just wrong, it's completely irrelevant.

Well, it's not irrelevant. Strings can be configured to handle large numbers.

Well, it's not irrelevant. Strings can be configured to handle large numbers.

Will both you guys please stop fighting and try helping me out?
I got so frustrated over the issue and did it in PYTHON. But Yes , there is a wqay of using srings to handle large integers but I guess my compiler is the culprit. Guys. - Let me know of a decent compiler and where to download it

I don't think downloading a compiler will help. You must write those functions.

I don't think downloading a compiler will help. You must write those functions.

It seems to be working perfectly for you. what compiler or IDE are you using?

What i meant was we can't directly use those functions and we must write those, i meant it is not compiler based but user based. if you want i'll send those codes

commented: wrong -2

ok. please do

I got so frustrated over the issue and did it in PYTHON. But Yes , there is a wqay of using srings to handle large integers but I guess my compiler is the culprit. Guys. - Let me know of a decent compiler and where to download it

there absolutely is a way to do it with 64-bit integers. C99 standard defines the 64-bit integer as the long long int data type.

i do not have any problem using this data type with my compiler, GCC version 3.4. I believe GCC current version is up to 4.3.

apparently, although i have not verified this personally, Windows and Borland compilers define the long long int as __int64 ...

http://home.att.net/~jackklein/c/inttypes.html#long_long

now you can either listen to some clown who wants you to create a string-based big integer handler from scratch, or you can use the 64-bit integer data type that is defined in the C99 standard. your current compiler may not adhere to this standard, but there are plenty that do.

http://www.codeblocks.org/
http://gcc.gnu.org/
http://www.mingw.org/


.

Actually i use c++ here. verylong class stores numbers as strings of digits.
The header file verylong.h

# include<iostream.h>
# include<string.h>
# include<stdlib.h>
const int SZ=200;
class verylong
{
private:
char vlstr[SZ];
int vlen;
verylong multidigit(int);
verylong mult10(verylong);
public:
verylong()
{
vlstr[0]='\0';
vlen=0;
}
verylong(char s[SZ])
{
strcpy(vlstr,s); vlen=strlen(s);
verylong(unsigned long n)
{
ltoa(n,vlstr,10);
strrev(vlstr);
}
void putvl();     //display verylong
void getvl();     //get verylong from user
verylong operator + (verylong);//Adds verylongs 
verylong operator * (verylong); //multiply verylongs
};
commented: This is a C forum, not c++. -7

And verylong.cpp which holds the member function definitions.

# include "verylong.h"

void verylongz::putvl()
{
char temp[SZ];
strcpy(temp,vlstr);
cout<<strrev(temp);
}
void verylong::getvl()
{
cin>>vlstr;
vlen=strlen(vlstr);
strrev(vlstr);
}
verylong verylong::operator + (verylong v) //Adds verylongs
{
char temp[SZ];
int maxlen=(vlen>v.vlen)?vlen:v.vlen;  //find longest number
int carry=0;
for(int j=0;j<maxlen;j++)
{
int d1=(j>vlen-1)?0:vlstr[j]-'0';
int d2=(j>v.vlen-1)?0:v.vlstr[j]-'0';
int digitsum=d1+d2+carry;
if(digitsum>=10)
{
digitsum-=10;carry=1;
}
else
carry=0;
temp[j]=digitsum+'0';
}
if(carry==1)
temp[j++]='1';
temp[j]='\0';
return verylong(temp);
}
verylong verylong:: operator * (verylong v) //multiply verylongs
{
verylong pprod;
verylong tempsum;
for(int j=0;j<v.vlen;j++)
{
int digit=v.vlstr[j]-'0';
pprod=multidigit(digit);
for(int k=0;k<j;k++)
pprod=mult10(pprod);
tempsum+=pprod;
}
return(tempsum);
}
verylong verylong::mult10(verylong v) //multiply by 10
{
char temp[SZ];
for(int j=v.vlen-1;j>=0;j--)
temp[j+1]=v.vlstr[j];
temp[0]='0';
return verylong(temp);
}
verylong verylong::multdigit(int d2)  /*multiply this verylong by a digit in argument*/
{
char temp[SZ];
int carry=0;
for(int j=0;j<vlen;j++)
{
int d1=vlstr[j]-'0';
int digitprod=d1*d2l
digitprod+=carry;
if(digitprod>=10)
{
carry=digitprod/10;
digitprod-=carry*10;
}
else carry=0;
temp[j]=digitprod+'0';
}
if (carry!=0)
temp[j++]=carry+'0';
temp[j]='\0';
return verylong(temp);
}

Will both you guys please stop fighting and try helping me out?

Agree.

but I guess my compiler is the culprit. Guys. - Let me know of a decent compiler and where to download it

You never did say what compiler you are using, or even what operating system (*nix, MS-Windows, or something else)

If MS-Windows, then google for Code::Blocks or for VC++ 2008 Express. Both are excellent compilers, and there are others too. Stay clear of Turbo C because its just simply too ancient.

Well, it's not irrelevant. Strings can be configured to handle large numbers.

For the purpose of the discussion in this thread the OP does not need to resort to writing string functions to work with those huge numbers -- the C language already has 64-bit integers which are more than suffient for the OPs use. If he needed something larger than what can be held in a 64-bit integer (such as the national debt of the USA government) then you are right about those string functions. I also think there are existing libraries that handle integers of unlimited size.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.