0

I'm currently learning about GMP in a course about scientific programming. I need to print the table of 23^45. This is a unusual big number, so I need to work with GMP.

I have to problem with constructing a loop from 1 to 10. The trouble lies in assingning 23^45 to a variable. I searched for it online, but I couldn't find anything simillar.

So far I know that first the mpz_t variables need to be defined. Then memory needs to be allocated for the variable. The next step is to assign the values to the variables.

I already read this documentation: https://tspiteri.gitlab.io/gmp-mpfr-sys/gmp/Integer-Functions.html

Therefore I know that I can assign a value with mpz_set_str(variable, "str", base)

or with mpz_set_d(variable, value to assign).

So far I have:

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

int main(void)
{

   mpz_t a,b;


   // allocate memory for the variables
   mpz_init(a);
   mpz_init(b);

I want to achieve something like this:

// assign values
    
mpz_set_d(a,pow(23,45));
mpz_set_str(b,"pow(23,45)",10)

From here I'm confused and I haven't been able to find an example. It would be great if anybody could help me or direct me to a simmilar thread.

Ter

UPDATE:

I now have this piece of code:

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

int main(void)
{

   mpz_t a,b,c,d;

   mpz_init(c);

   mpz_init_set_ui(a,23);
   mpz_init_set_ui(b,45);

   mpz_pow_ui(c,a,b);

   gmp_printf("a: %Zd , b: %Zd, 23^45: %Zd",a,b,c);





   return EXIT_SUCCESS;
}

But I'm still not there, since I have a overflow.

Question: Why do I get a overflow Question: What do I need to change in order to avoid the overflow?

Tim
  • 415
  • 2
  • 10
  • 3
    Gmp has exponentiation functions. They are mentioned on the page you linked to. – molbdnilo Sep 15 '20 at 16:42
  • 1
    You can initialise from a string with [`int mpz_set_str (mpz_t rop, const char *str, int base)`](https://gmplib.org/manual/Assigning-Integers#Assigning-Integers). – Weather Vane Sep 15 '20 at 16:44
  • Yes, but how should I write that string down? I cannot write out 23^45 (in a reasonable time haha) – Tim Sep 15 '20 at 16:46
  • *how should I write that string down? I cannot write out 23^45 (in a reasonable time* Why not? It's not that long and would take only a minute or so. – doug Sep 15 '20 at 16:56
  • Could you please give an example? I would write the string out as "pow(23,45) or 23*23*23..........*23", but the first one is incorrect. The second one I don't know, but isn't there a smarter way to do it?. I also tried mpz_ui_pow_ui(a,23,45). Could anybody please give an example – Tim Sep 15 '20 at 16:59
  • You obviously can't do 23^45 as regular int, it won't fit. So create two mpz values and take power. – stark Sep 15 '20 at 17:09
  • @stark I tried what you said, but I get an overflow. Could you please give me feedback? – Tim Sep 15 '20 at 17:42
  • Looks like 23^45 is too big. See https://stackoverflow.com/q/13328409/1216776 – stark Sep 15 '20 at 17:52
  • What do you need that number for? Worst case, write a loop to multiply 1 by 23, 45 times. – stark Sep 15 '20 at 18:12
  • The number is part of the assignment. – Tim Sep 15 '20 at 18:14
  • @stark The number is not too big. I tried 3^2 and still the same error – Tim Sep 15 '20 at 18:35
  • 2
    The exponent passed to `mpz_pow_ui` has to be an `unsigned long int`, not an `mpz_t`. Your compiler should complain about this if you increase the warning level. – f9c69e9781fa194211448473495534 Sep 15 '20 at 19:20

1 Answers1

0

By definition (GMP 6.2.1 manual)

void mpz_pow_ui (mpz t rop, const mpz t base, unsigned long int exp)

so, try to replace:

mpz_t a,b,c,d;
mpz_init_set_ui(b,45);

for:

mpz_t a,c,d;
unsigned long int b = 45;

you may need to change your gmp_printf statement accordantly.

hope helps

Askold Ilvento
  • 1,405
  • 1
  • 17
  • 20