Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Result for number aboove its range (http://www.go4expert.com/forums/result-aboove-range-t13413/)

sakshi02 30Aug2008 14:09

Result for number aboove its range
 
Hey can anyone tell me, that if i want to make a program which test whether a no is prime or nt for all integers and also for nos above the range of integer. Is there any way. Do i need to set my own range. pls tell me a way.

xpi0t0s 31Aug2008 00:42

Re: Result for number aboove its range
 
There are no (finite) numbers beyond the range of integer. Do you mean the int type? If so you may be able to extend the range by using long instead of int. Check your compiler documentation for how large the different integer types are; the standard only mandates that short <= int <= long, so all integer types could be 32-bit.

If no built in types fit the range you want to test then you'll have to define your own type and encode the operations you want to use. This isn't as difficult as it sounds; there's already an example of how we normally do that in this reply, i.e. the number 32 - no single digit has a value greater than 9, so we use multiple digits to represent numbers larger than 9. So you can use multiple ints to represent numbers larger than 2^(8*sizeof int), for example int bignum[4] will represent a number up to 2^(4*8*sizeof int).

Then you will need to write code to add these, because the built in operators only know about built in types. Again this is fairly easy; just remember how you did it on paper then write code to do that, for example:
1234
5678+
====

4+8=12, so write 2 below 4 and 8 and carry the 1. 3+7=10, add the carry=11, write 1, carry 1.
2+6=8, add carry=9, write 9. 1+5=6.
So in code you might have something like (simplified):
Code:

int bignum1[4]={1,2,3,4};
int bignum2[4]={5,6,7,8};
int bugnum3[4]={0,0,0,0}; // this will contain the sum
int carry=0;
for (int i=3; i>=0; i--) {
  bignum3[i]=bignum1[i]+bignum2[i]+carry;
  carry=0;
  if (bignum3[i]>10) {
    bignum3[i]-=10;
    carry=1;
  }
}


sakshi02 31Aug2008 16:11

Re: Result for number aboove its range
 
What i did is I create a func atoi1 as i want in long long
so i wrote
unsigned long long atoi1(char s[])
{
unsigned long long m=0;
int i;
for(i=0;s[i]>='0' && s[i]<='9';++i)
m=10*m+(s[i]-'0');
return m;
}
in main i create an array which takes the no string from user
and with the help of this func it converts it into long long
again it is giving negative result on exceeding the value from 32767
ie 16 bit

xpi0t0s 31Aug2008 16:51

Re: Result for number aboove its range
 
How are you displaying the return value?
I tried the above code and it worked fine with 1048576, using Visual Studio 2005.

sakshi02 31Aug2008 16:56

Re: Result for number aboove its range
 
i did in TC. and i wrote printf("%d",m);
oh i should try %ld, is this the prob. let me check it on visual studio


All times are GMT +5.5. The time now is 16:32.