C++ में बड़े नंबर्स का फैक्टोरियल कैलकुलेट करने के लिए Boost.Multiprecision लाइब्रेरी का cpp_int टाइप उपयोग करें। यह आर्बिट्ररी प्रिसिजन इंटीजर प्रदान करता है जो रनटाइम पर आवश्यकतानुसार बढ़ता है।
उदाहरण कोड
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
using namespace std;
cpp_int factorial(int n) {
cpp_int result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
int main() {
// छोटे नंबर्स
for (int i = 1; i <= 10; ++i) {
cout << i << "! = " << factorial(i) << endl;
}
cout << "\n=== बड़े फैक्टोरियल्स ===\n";
cout << "20! = " << factorial(20) << endl;
cout << "50! = " << factorial(50) << endl;
cout << "100! = " << factorial(100) << endl;
// डिजिट्स काउंट
auto fact100 = factorial(100);
string s = fact100.convert_to<string>();
cout << "\n100! has " << s.length() << " digits\n";
return 0;
}
आउटपुट
1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 === बड़े फैक्टोरियल्स === 20! = 2432902008176640000 50! = 30414093201713378043612608166064768844377641568960512000000000000 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 100! has 158 digits
अन्य उपयोगी टाइप्स
#include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> using namespace boost::multiprecision; // फिक्स्ड प्रिसिजन इंटीजर्स (फास्टर दैन cpp_int) int128_t a = 1; // 128 बिट int256_t b = 1; // 256 बिट int512_t c = 1; // 512 बिट int1024_t d = 1; // 1024 बिट // आर्बिट्ररी प्रिसिजन फ्लोटिंग पॉइंट cpp_dec_float_50 pi = "3.14159265358979323846264338327950288419716939937510"; cpp_dec_float_100 pi100 = "3.14..."; // 100 डिजिट्स // रेशनल नंबर्स #include <boost/rational.hpp> boost::rational<cpp_int> r(cpp_int(22), cpp_int(7)); // 22/7
कंपाइलेशन
# Boost हेडर-ओनली है, लिंकिंग की जरूरत नहीं g++ -std=c++17 -O2 -I/usr/include/boost factorial.cpp -o factorial # या CMake में find_package(Boost REQUIRED COMPONENTS multiprecision) target_link_libraries(your_target Boost::multiprecision)
परफॉरमेंस नोट्स
cpp_int: आर्बिट्ररी प्रिसिजन, डायनामिक मेमोरी अलोकेशन - धीमाint128_t,int256_t: फिक्स्ड साइज, स्टैक पर - तेज- ज्ञात अधिकतम साइज के लिए फिक्स्ड-विड्थ टाइप्स प्रीफर करें
cpp_dec_floatफ्लोटिंग पॉइंट के लिए
एप्लीकेशन्स
- क्रिप्टोग्राफी (RSA, ECC बड़े प्राइम्स)
- कॉम्बिनेटोरिक्स (बड़े फैक्टोरियल्स, बाइनोमियल कोएफिशिएंट्स)
- हाई-प्रिसिजन साइंटिफिक कंप्यूटिंग
- फाइनेंशियल कैलकुलेशन्स (एग्जैक्ट डेसीमल)