यहां हम देखेंगे कि अंतिम दो अंक कैसे प्राप्त करें। N फैक्टोरियल के योग का इकाई स्थान अंक और दहाई स्थान अंक। यदि N=4 है, तो यह 1! + 2! + 3! + 4! = 33 होगा। इसलिए इकाई स्थान 3 और दहाई स्थान 3 है। परिणाम 33 होगा।
यदि हम इसे स्पष्ट रूप से देखें, तो N > 5 के फैक्टोरियल के लिए, इकाई स्थान 0 होता है, इसलिए 5 के बाद यह इकाई स्थान को बदलने में योगदान नहीं देगा। और N > 10 के बाद, दहाई स्थान भी 0 रहेगा। N=10 और अधिक के लिए, यह 00 होगा। हम N=1 से 10 तक के फैक्टोरियल संख्याओं का चार्ट बना सकते हैं।
हम इस समस्या को इन चरणों का उपयोग करके हल कर सकते हैं:
- यदि n का मान 10 से कम है, तो (1! + 2! + ... + n!) mod 100
- अन्यथा जब n का मान 10 से अधिक या बराबर है, तो (1! + 2! + ... + 10!) mod 100 = 13
C++ उदाहरण
#include <iostream>
#include <cmath>
using namespace std;
int getTenAndUnitPlace(long long N) {
if (N <= 10) {
long long ans = 0, factorial = 1;
for (int i = 1; i <= N; i++) {
factorial *= i;
ans += factorial;
}
return ans % 100;
}
return 13;
}
int main() {
for (long long i = 1; i < 15; i++) {
cout << "Ten and Unit place value of sum of factorials when N = " << i
<< " is: " << getTenAndUnitPlace(i) << endl;
}
return 0;
}
आउटपुट
Ten and Unit place value of sum of factorials when N = 1 is: 1 Ten and Unit place value of sum of factorials when N = 2 is: 3 Ten and Unit place value of sum of factorials when N = 3 is: 9 Ten and Unit place value of sum of factorials when N = 4 is: 33 Ten and Unit place value of sum of factorials when N = 5 is: 53 Ten and Unit place value of sum of factorials when N = 6 is: 73 Ten and Unit place value of sum of factorials when N = 7 is: 13 Ten and Unit place value of sum of factorials when N = 8 is: 33 Ten and Unit place value of sum of factorials when N = 9 is: 13 Ten and Unit place value of sum of factorials when N = 10 is: 13 Ten and Unit place value of sum of factorials when N = 11 is: 13 Ten and Unit place value of sum of factorials when N = 12 is: 13 Ten and Unit place value of sum of factorials when N = 13 is: 13 Ten and Unit place value of sum of factorials when N = 14 is: 13