Prolem with Integer Division

arthur arthur at ilvsun3.com
Sat Feb 6 15:49:53 CET 2010


Howdy Jack:

If you change
 >         gmp_printf("%lu \n", n);
to
 >         gmp_printf("%Zd \n", n);

your program likely works as desired.
With input 1234, it prints:
This is an even number, so 1234 = 2 x 617

/arthur

jack THE RED wrote:
> Hi everyone!
> I'm new to GMP and I have a problem regarding the integer division.
> I did read the manual and tried a lot of the division function.
> 
> But the result is always something weird:
> "so 1234 = 2 x 3218735876"
> Can someone tell me what is the problem?
> 
> Code:
> 
> #include <stdio.h> 
> #include <gmp.h>
> 
> 
> int main(){ 
>     mpz_t n; 
>     mpz_t root;
>     mpz_t remainder;
>     mpz_t s;
>      
>     mpz_init (n);
>     mpz_init (root);
>     mpz_init (remainder);
>     mpz_init (s);
>      
>     printf("Please enter a number to by two: \n"); 
>     gmp_scanf ("%Zd",&n); 
> 
>     gmp_printf("number inputed %Zd  \n", n);
>     mpz_sqrt(root,n);
>     gmp_printf("number root %Zd  \n", root);
>     mpz_set(s,n);
>     mpz_mod_ui(remainder,s,2);//remainder <- n%2
>     if (mpz_cmp_ui(remainder,1)){// if remainder!=1 (means that n is an even number)
>  
>         gmp_printf("This is an even number, so %Zd = 2 x ", n);
>         mpz_div_ui(n, n, 2); /// Error here! 
>         gmp_printf("%lu \n", n);
>         }
>     
>     mpz_clear (n);
>     mpz_clear (root);
>     mpz_clear (remainder);
>     mpz_clear (s); 
>     return 0; 
> } 		 	   		  


More information about the gmp-discuss mailing list