কম্পিউটার টিউটোরিয়াল

C++ ব্যবহার করে N ফ্যাক্টোরিয়ালের যোগফলের শেষ দু ডিজিট বের করা

এখানে আমরা দেখব কিভাবে N ফ্যাক্টোরিয়ালের যোগফলের শেষ দু ডিজিট (দশমিক ও একক স্থান) বের করতে হয়। N = 4 হলে 1! + 2! + 3! + 4! = 33, একক ৩ দশমিক ৩, ফলাফল ৩৩।

স্পষ্টতঃ দেখা যায় N > 5 হলে ফ্যাক্টোরিয়ালের একক দশমিক ০, তাই ৫ এর পর একক দশমিক পরিবর্তন হয় না। আর N > 10 হলে দশমিক স্থানও ০ থাকে। N = 10 বা তার বেশি হলে ফলাফল ০০। N = 1 থেকে ১০ পর্যন্ত ফ্যাক্টোরিয়াল নম্বরের একটি চার্ট বানানো যায়।

এটি নিচের ধাপগুলো দিয়ে সমাধান করা যায়:

  • যদি n এর মান ১০-এর চেয়ে ছোট হয়, তবে (1! + 2! + ... + n!) mod 100
  • অন্যথায় যখন n ≥ 10, তবে (1! + 2! + ... + 10!) mod 100 = 13

উদাহরণ কোড

#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 = 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;
    }
}

আউটপুট

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
  1. C++ ব্যবহার করে N ফ্যাক্টোরিয়ালের যোগফলের একক দশমিক স্থান বের করা

  2. C++ ব্যবহার করে এমন একটি সংখ্যা x বের করা যাতে x এবং তার ডিজিটের যোগফল n-র সমান হয়

  3. রিকার্সন ব্যবহার করে প্রাকৃতিক সংখ্যাগুলোর যোগফল (Sum of Natural Numbers) বের করার C++ প্রোগ্রাম

  4. পাইথনে বেস K-এ রূপান্তরিত সংখ্যার ডিজিট সম গণনা