Thread: Problem with GMP

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    16

    Problem with GMP

    Hello guys, i am trying to solve Project eular problem-20 :"Find the sum of the digits in the number 100!".So i downloaded and included gmp libary for big integer.This is my code:
    Code:
    #include <stdio.h>
    #include <mpir.h>
    int main()
    {
    unsigned long s=0;
    
    mpz_t result;
    mpz_init (result);
    mpz_fac_ui (result,10);
    while(result>0)   //untill here everything works fine
    {
            s+=mpz_tdiv_r_ui (result, result, 10); 
            mpz_tdiv_q_ui (result, result, 10); 
    }
    printf ("%lu", s);
    return 0;
    }
    I am definetly having problems with while loop and instructions in it,and i just dont know how to solve it. The reason why i used 10!, its just a test.
    Thank you for your time.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can't use any "normal" operations on mpz_t's. So you'll have to replace result>0 with whatever function GMP has for that.

    Actually, GMP is overkill for this since it basically just does it for you. You should do it yourself with plain old C.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    Quote Originally Posted by oogabooga View Post
    You can't use any "normal" operations on mpz_t's. So you'll have to replace result>0 with whatever function GMP has for that.

    Actually, GMP is overkill for this since it basically just does it for you. You should do it yourself with plain old C.
    Thank you i did it , and also i didnt need two insructions only one
    Code:
    s+=mpz_tdiv_q_ui (result,result, 10);
    .
    And instead while i did for loop that goes from 1 to 300 (i suppose that number is not longer) , but still i should sreach and find special function in gmp for result>0.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Looks like this is what you need:
    int mpz_cmp_ui (mpz_t op1, unsigned long int op2) Macro
    Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2.
    Code:
    while (mpz_cmp_ui(result,0) > 0)

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    Quote Originally Posted by oogabooga View Post
    Looks like this is what you need:
    int mpz_cmp_ui (mpz_t op1, unsigned long int op2) Macro
    Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2.
    Code:
    while (mpz_cmp_ui(result,0) > 0)
    How did i miss that? Thank you, it works like a charm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM