I have a problem with unsigned long long int's. I'll tried to do a function wich calculates the factors of an integer. It worked fine with int, but when an tried with unsigned long long int the % and <= operation failed...
here is the surce code for the function

void factor(unsigned long long int X){
      unsigned long long int i;
      int j=0;
      unsigned long int fcts[100];


       while(X % 2 ==0){

                  if(X % 2 ==0){
                        X = X / 2;
                        fcts[j] = 2;
                        j++;
                        printf("faktor %lu och x %lu \n", 2, X);

                  }
      }

      for(i=3; i<=X ; i+=2){
            
            
            while(X % i ==0){

                  if(X % i ==0){

                        X = X / i;
                        fcts[j] = i;
                        j++;
                        printf("faktor %lu och x %lu \n", i, X);

                  }
            }
      }

      for(i=0; i<=j-1; i++){

      printf("%d  ", fcts[i]);
      }

}

the forloop should end when i is bigger then X but i doesn't.....Does any one know whats wrong?
thanx!

Recommended Answers

All 9 Replies

line 4 is declared wrong. should be long long , not long int To my knowledge there is not such thing as long int

All the printf formats for the data types you're trying to print are also wrong.

Also, tell us which OS/Compiler you're using because "long long" is a C99 feature, and support for that is not that widespread.

For the first the long declaration is correct but quiet unessesery because long is the same as int(cheked with the ANSI standart) so no problem there. With the printf's; I have changed them back and forth because I thought that that might have been the problem. But it isn't the case. It might be as you say a problem with the OS. I have the bloodshed DEV C++ v4 and run it on a win xp 32bit. The problem with the program is that the loop(line 18-32) continues to run way after i is bigger than X. Any suggestions? Thanx

> because long is the same as int(cheked with the ANSI standart) so no problem there.
The standard makes no such guarantee.
The standard only mandates MINIMUM sizes for various types. In the case of int, that's 16 bits and for long, it's 32 bits. That int and long are the same size on your machine is a happy coincidence.

> I have the bloodshed DEV C++ v4 and run it on a win xp 32bit.
Excellent information.
http://msdn2.microsoft.com/en-us/library/56e442dc(VS.71).aspx
Although Dev-C++ uses the GCC compiler, it also uses the Microsoft run-time. Which means you have the bugs and portability issues of that run-time to deal with.

unsigned long long int foo = 1234;
printf( "%I64u\n", foo );

Thanx a lot! It helped! Now it's running godd for fairly big integers. But with large numbers still way under 2^64 there are strange results as for example the number 9999999997 had the factors 5*7^2*29*19846 wich is clerly wrong because it can never be divided by 5. Is there a way of solving that problem and what is the cause? I'm interested in how to handle realy large integers. Thanx!

There is no support in standard C or C++ for huge numbers beyone 64-bit integers. But you can do some googleing to see if you can find a math library that will supply such functions, or write your own by using character arrays to hold the digits.

>>For the first the long declaration is correct but quiet unessesery
My compiler, VC++ 2008 Express, produced an error or warning on that line. Deleting the int part of that line fixed it. Your compiler probably didn't complain.

I still don't understand why it's calculating 9999999997 wrong. It's much smaller than 64bit's. So there shouldn't be any problem. Is there any limit for what the % , armithmetic and evaluation-operations can handle?
Because I thought of writing my own type to hold the big integers but first I want my function to work with the standard types.

This is the code with some changes suggested by you and it works fine with numbers aprox smaller than 32bit..

void factor(unsigned long long int X){
      unsigned long long int i = 2,fcts[100];
      int j = 0;

       while(X % i == 0){

                  if(X % i == 0){
                        X = X / i;
                        fcts[j] = i;
                        j++;
                        printf("faktor %d och x %I64u \n", 2, X);

                  }
      }

      for(i = 3; i <= X ; i += 2){

            while(X % i == 0){

                  if(X % i == 0){

                        X = X / i;
                        fcts[j] = i;
                        j++;
                        printf("faktor %I64u och x %I64u \n", i, X);

                  }
            }
      }
      for(i = 0; i< = j-1; i++){
      printf("%I64u  ", fcts[i]);
      }
}

Thanx for all the help:)!!

line 30: remove the space between < and =

This is what I get when passed 9999999997 to that function. That is the correct answer.

Enter a number ...9999999997 <<<<< I added this in main() so I could test other numbers
faktor 13 och x 769230769
faktor 769230769 och x 1
13 769230769 Press any key to continue . . .

Thanx a lot for helping me:) I found the problem; Ive forgot to change the declaration of the variable that was sent in to the function. Now i also get the correct answer:)
Thanx again!

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.