Really big number - c++

First of all apologies if there is already a topic like this but I have not found... I need to know how to handle a really big number such as the result of 789^2346:
#include <iostream>
#include <cmath>
using namespace std;
int main () {
cout << pow(789,2346) << endl;
}

You could try the GNU MP Bignum Library or ttmath. This link point to some samples. It is very easy to use.

You need a "big number" library. A popular choice is GNU's Multiple Precision Arithmetic Library, which has a C interface. I's also been around for a while. Another one, for C++, is Big Integer Library.
I'm sure there is a list of bignum libraries on SO somewhere, but I cannot find it. There is a tag you could stroll through.

You can consider NTL (Number Theory Library) for C++ - http://www.shoup.net/ntl/ . It's very easy to use.
If you can relax C++ requirement, Perl and Python support big integers natively. PHP supports via bcmath or gmp extensions.

Related

cout causes compile error when using __float128 (error: ambiguous overload for ‘operator<<’) [duplicate]

In my question about Analysis of float/double precision in 32 decimal digits, one answer said to take a look at __float128.
I used it and the compiler could find it, but I can not print it, since the complier can not find the header quadmath.h.
So my questions are:
__float128 is standard, correct?
How to print it?
Isn't quadmath.h standard?
These answers did not help:
Use extern C
Precision in C++
Printing
The ref also did not help.
Note that I do not want to use any non standard library.
[EDIT]
It would be also useful, if that question had an answer, even if the answer was a negative one.
Work in GNU-Fortran! It allows to run the same program in different precision: single (32 bit), double (64 bit), extended (80 bit) and quad (128 bit). You don't have to do any changes in the program, you simply write 'real' for all floating points. The size of floating points is set by compiler options -freal-4-real-8, -freal-4-real-10 and -freal-4-real-16.
Using the boost library was the best answer for me:
#include <boost/multiprecision/float128.hpp>
#include <boost/math/special_functions/gamma.hpp>
using namespace boost::multiprecision;
float128 su1= 0.33333333333333333q;
cout << "su1=" << su1 << endl;
Remember to link this library:
-lquadmath
No, it's not standard - neither the type nor the header. That's why the type has a double underscore (reserved name). Apparently, quadmath.h provides a quadmath_snprintf method. In C++ you would have used <<, of course.

Declare a Big Integer in C++ [duplicate]

This question already has answers here:
STL BigInt class implementation
(3 answers)
Closed 7 years ago.
I am trying to run this algorithm in c++ in order to get a big number
#include<iostream>
using namespace std;
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}
How can I declare a Big Integer like in Java instead of an int?
There is no big-integer support in the C++ standard library. A common choice for big-number arithmetic is GMP. After downloading and installing the library, in your code you would #include <gmpxx.h> and declare mpz_class factorial instead of int factorial, then link against GMP.
Linking with GMP can be done in an IDE, by adding GMP in your editor’s compile settings; or by adding -lgmp to your compilation command (e.g., g++ or clang++).
There is no arbitrary-precision arithmetic in c++ standard library. You'll need to implement it yourself using an array of integers, or use an existing non-standard library.
Try unsigned long long int for just convenience.
I just want to leave it as comment but I have low repu for commenting..
There is no standard support for arbitrary-precision integers. However, a few libraries are available for handling big integers:
GNU MP Bignum Library
InfInt
C++ BigInt class
BigDigits multiple-precision arithmetic (in C)
BigIntegerCPP (only supports addition and multiplication)
C++ Big Integer Library (no longer maintained)

Print __float128, without using quadmath_snprintf

In my question about Analysis of float/double precision in 32 decimal digits, one answer said to take a look at __float128.
I used it and the compiler could find it, but I can not print it, since the complier can not find the header quadmath.h.
So my questions are:
__float128 is standard, correct?
How to print it?
Isn't quadmath.h standard?
These answers did not help:
Use extern C
Precision in C++
Printing
The ref also did not help.
Note that I do not want to use any non standard library.
[EDIT]
It would be also useful, if that question had an answer, even if the answer was a negative one.
Work in GNU-Fortran! It allows to run the same program in different precision: single (32 bit), double (64 bit), extended (80 bit) and quad (128 bit). You don't have to do any changes in the program, you simply write 'real' for all floating points. The size of floating points is set by compiler options -freal-4-real-8, -freal-4-real-10 and -freal-4-real-16.
Using the boost library was the best answer for me:
#include <boost/multiprecision/float128.hpp>
#include <boost/math/special_functions/gamma.hpp>
using namespace boost::multiprecision;
float128 su1= 0.33333333333333333q;
cout << "su1=" << su1 << endl;
Remember to link this library:
-lquadmath
No, it's not standard - neither the type nor the header. That's why the type has a double underscore (reserved name). Apparently, quadmath.h provides a quadmath_snprintf method. In C++ you would have used <<, of course.

Fortran-style multidimensional arrays in C++

Is there a C++ library which provides Fortran-style multidimensional arrays with support for slicing, passing as procedural parameter and decent documentation? I've looked into blitz++ but its dead!
I highly suggest Armadillo:
Armadillo is a C++ linear algebra library (matrix maths) aiming towards a good balance between speed and ease of use
It is a C++ template library:
A delayed evaluation approach is employed (at compile-time) to combine several operations into one and reduce (or eliminate) the need for temporaries; this is automatically accomplished through template meta-programming
A simple example from the web page:
#include <iostream>
#include <armadillo>
int main(int argc, char** argv)
{
arma::mat A = arma::randu<arma::mat>(4,5);
arma::mat B = arma::randu<arma::mat>(4,5);
std::cout << A*B.t() << std::endl;
return 0;
}
If you are running OSX the you can use the vDSP libs for free.
If you want to deploy on windows targets then either license the intel equivalents (MKL) or I think that the AMD vector math libs (ACML) are free.

How to calculate a SHA-512 hash in C++ on Linux?

Is there a standard library or commonly used library that can be used for calculating SHA-512 hashes on Linux?
I'm looking for a C or C++ library.
Have you checked OpenSSL. I myself have not used it but documentation says it supports it.
Here is list of few more implementations.
Example code
md = EVP_get_digestbyname("sha512");
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
Check this code. It is fully portable and does not need any additional configurations. Only STL would suffice. You'll just need to declare
#include "sha512.hh"
and then use the functions
sw::sha512::calculate("SHA512 of std::string") // hash of a string, or
sw::sha512::file(path) // hash of a file specified by its path, or
sw::sha512::calculate(&data, sizeof(data)) // hash of any block of data
whenever you need them. Their return value is std::string
I'm using Botan for various cryptographic purposes. It has many kinds of SHA(-512) algorithms.
When I was looking at C++ crypto libraries I also found Crypto++. The style of the Botan API was more straightforward for me, but both of these libraries are solid and mature.
I have had great success with this:
Secure Hash Algorithm (SHA)
BSD license. It covers SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512. It has neat helper functions reduce steps for simple cases:
SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH])
It also has a lot of performance tuning options.