Power Function at GMP Library

I recently downloaded GMP Library for big num. Everything worked fine but when i'm trying to use the pow(2, x) where the x is a mpz_class it wont work.

And i'm not sure about the gmp function of mpq_ui_pow_ui or mpq str .
any idea how can i do a 2 to the power of big num?
2 to the power of a big num is going to be a VERY big num! Think about 2 to the power of LONG_MAX+1. Are you sure that's what you need? How is the result used?

In any case, GMP doesn't raise anything to bignum exponents: it can raise unsigned long to unsigned long and store the result in a bignum (mpz_ui_pow_ui), it can raise a bignum to an unsigned long power (mpz_pow_ui), or it can raise a bignum to a bignum power MOD another bignum (mpz_powm).

And yes, neither power nor arithmetic left shift are available for mpz_class objects, and yes you have to call the C interface:

1
2
3
4
5
6
7
8
9
#include <gmpxx.h>
#include <iostream>
int main()
{
    mpz_class r;
    mpz_ui_pow_ui (r.get_mpz_t(), 2, 123);

    std::cout << 2 << "^" << 123  << " = " << r << '\n';
}

2^123 = 10633823966279326983230456482242756608
Last edited on
i'm actually trying to make a program that can find the largest prime number.
but i need to divide the mpz_class with 2 int and it shows:
ambigous overload for 'operator=' in '.... =(..../...)'

here's the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <string>
#include <gmpxx.h>
#include <cmath>
using namespace std;

    typedef mpz_class a;

int main()
{
    SetConsoleTitle(L"Prime Number Finder");

    clock_t start;
    unsigned long long isPrime, sqrtPrime, addPrime = 3, ansPrime,dPrime = 0.0;
    a dans;



    cout << "Please insert a number to test as prime number\n";
    cout << "Insert a power of 2 \n";
    cin >> isPrime ;
    mpz_ui_pow_ui(dans.get_mpz_t(), 2, isPrime);
   // isPrime = pow(2 ,isPrime) - 1;
   if(isPrime==0){

        cout << "Please insert a valid number !!! \a \n ";
                }

    start = clock();
    sqrtPrime = sqrt(isPrime);

if(addPrime<sqrtPrime){
    while ( addPrime<sqrtPrime){

        ansPrime = isPrime%addPrime ;
        dPrime = addPrime;
        dans = isPrime/dPrime;
        if(ansPrime == 0){
            cout << endl << "Fail \a \n " << endl;
            break;
        }
        addPrime += 2 ;

    }}else{
        cout << "Failed Number. Please Try Again. \n";
    }

        start = clock() - start;
        cout << endl << "Time used to calculate is " << start << " seconds" << endl;
        cout << "The number you've entered is " << isPrime << endl;
        if(addPrime<sqrtPrime){
        cout << "Your Number is Divisible by is " << addPrime << endl;
        cout << "The Divided Answer is " << dans << endl;
        }

    getch();
    return 0;

}
i'm actually trying to make a program that can find the largest prime number.
Let P(n) be the set of all positive prime numbers <=n. Note that P(n) is finite. Let
m = 1 + \product_{i \in P(n)} i
m is indivisible by all i in P(n), therefore (fundamental theorem of algebra) there exists a prime number larger than n. In other words, for any natural number, there exists a prime number that is larger than it. All positive prime numbers are natural, therefore for any prime number there exists at least one prime number that is larger. Therefore, no prime number is the largest.
Last edited on
not the question.. :D
any help?
We cannot help you find the largest prime number because it does not exist.
well, you 2 guys....
how bout when i enter a number and it will become 2^(the number)
and check if the number is a prime or not.?
Here you go :)

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
int main()
{
  std::string x;
  std::cout << "Enter the number to raise two to the power of: ";
  std::cin >> x;

  std::cout << " 2^" << x << " is NOT prime.";
}
Last edited on
2^x and -1
reply?
Last edited on
Topic archived. No new replies allowed.