কম্পিউটার

C++ এ মিডির থিওরেম বর্ধিত


মিডি'স থিওরেম হল একটি বিবৃতি যা n/p দ্বারা চিহ্নিত সংখ্যার দশমিক প্রসারণের জন্য ব্যবহৃত হয়, যেখানে n হল যেকোনো সংখ্যা এবং p হল একটি মৌলিক সংখ্যা এবং a/p এর জোড় পিরিয়ডের সাথে পুনরাবৃত্তি করা দশমিক আছে।

Extended Midy's Theorem-এ, পুনরাবৃত্তি করা অংশকে m সংখ্যায় ভাগ করা হয়, তারপর তাদের যোগফল 10m - 1 এর গুণিতক হয়।

বর্ধিত মিডির উপপাদ্য চিত্রিত করার জন্য প্রোগ্রাম:

উদাহরণ

#include <bits/stdc++.h>
using namespace std;

string findDecimalValue(int num, int den) {
   
   string res;
   unordered_map<int, int> mp;
   int rem = num % den;

   while ((rem != 0) && (mp.find(rem) == mp.end())) {

      mp[rem] = res.length();
      rem = rem * 10;
      int part = rem / den;
      res += to_string(part);
      rem = rem % den;
   }
   return (rem == 0) ? "-1" : res.substr(mp[rem]);
}

bool isPrime(int n) {
   
   for (int i = 2; i <= n / 2; i++)
      if (n % i == 0)
         return false;
   return true;
}

void ExtendedMidysAlgo(string str, int n, int m) {
   
   if (!isPrime(n)) {
      cout<<"Denominator is not prime, thus Extended Midy's theorem is not applicable";
      return;  
   }

   int l = str.length();
   int part1 = 0, part2 = 0;
   if (l % 2 == 0 && l % m == 0) {

      int part[m] = { 0 }, sum = 0, res = 0;
      for (int i = 0; i < l; i++) {
         int var = i / m;
         part[var] = part[var] * 10 + (str[i] - '0');
      }
      for (int i = 0; i < m; i++) {
         sum = sum + part[i];
         cout << part[i] << " ";
      }
      cout << endl;
      res = pow(10, m) - 1;
      if (sum % res == 0)
         cout << "Extended Midy's theorem holds!";      
      else
         cout << "Extended Midy's theorem doesn't hold!";      
   }
   else if (l % 2 != 0) {
      cout << "The repeating decimal is of odd length thus Extended Midy's theorem is not applicable";
   }
   else if (l % m != 0) {
      cout<<" The repeating decimal can not be divided into m digits";
   }
}

// Driver code
int main()
{
   int numr = 1, denr = 17, m = 4;
   string res = findDecimalValue(numr, denr);
   if (res == "-1")
      cout << "The fraction does not have repeating decimal";
   else {
      cout << "Repeating decimal = " << res << endl;
      ExtendedMidysAlgo(res, denr, m);
   }
   return 0;
}

আউটপুট −

Repeating decimal = 0588235294117647
588 2352 9411 7647
Extended Midy's theorem holds!

  1. C++ এ রেখার প্রতিফলন

  2. C++ এ ডায়াগোনাল ট্রাভার্স II

  3. বর্ধিত ইউক্লিডীয় অ্যালগরিদম বাস্তবায়নের জন্য C++ প্রোগ্রাম

  4. অয়লার উপপাদ্য বাস্তবায়নের জন্য C++ প্রোগ্রাম