If you're seeing this message, it means we're having trouble loading external resources on our website.

তোমার যদি কোন ওয়েব ফিল্টার দেওয়া থাকে, তাহলে দয়া করে নিশ্চিত কর যে *.kastatic.org এবং *.kasandbox.org ডোমেইনগুলো উন্মুক্ত।

মূল বিষয়বস্তু

একটি সংখ্যার ঘাত হিসাব করা

যদিও জাভাস্ক্রিপ্ট-এ একটি pow ফাংশন রয়েছে যা একটি নম্বরের সূচক গণনা করে, তুমি একই রকমের ফাংশনকে পুনরাবৃত্তিতে লিখতে পার এবং এটি খুব কার্যকর হতে পারে। একমাত্র বাধা হল যে সূচকটি একটি পূর্ণসংখ্যা হতে হবে।
ধর তুমি গণনা করতে চাও xn, যেখানে x যেকোন বাস্তব সংখ্যা এবং n কোনো পূর্ণসংখ্যা। এটা খুবই সহজ হয় যদি n এর মান 0 হয়, যেহেতু x0=1 যা x এর যেকোনো মানের জন্যই প্রযোজ্য। এটা বেস কেস হিসেবে ধরা যায়।
So now let's see what happens when n is positive. Let's start by recalling that when you multiply powers of x, you add the exponents: xaxb=xa+b for any base x and any exponents a and b. Therefore, if n is positive and even, then xn=xn/2xn/2. If you were to compute y=xn/2 recursively, then you could compute xn as yy. What if n is positive and odd? Then xn=xn1x, and n1 either is 0 or is positive and even. We just saw how to compute powers of x when the exponent either is 0 or is positive and even. Therefore, you could compute xn1 recursively, and then use this result to compute xn=xn1x.
What about when n is negative? Then xn=1/xn, and the exponent n is positive, since it's the negation of a negative number. So you can compute xn recursively and take its reciprocal.
এইসব পর্যবেক্ষণগুলোকে একসাথে করলে, Xn গণনা করার জন্য আমরা নিম্নলিখিত পুনরাবৃত্তমূলক অ্যালগরিদমটি পাই:
  • The base case is when n=0, and x0=1.
  • If n is positive and even, recursively compute y=xn/2, and then xn=yy. Notice that you can get away with making just one recursive call in this case, computing xn/2 just once, and then you multiply the result of this recursive call by itself.
  • If n is positive and odd, recursively compute xn1, so that the exponent either is 0 or is positive and even. Then, xn=xn1x.
  • If n is negative, recursively compute xn, so that the exponent becomes positive. Then, xn=1/xn.

এই বিষয়বস্তুটি Dartmouth Computer Science এর প্রফেসর Thomas Cormen এবং Devin Balkcom এর সহযোগিতায় এবং একই সাথে খান একাডেমির কম্পিউটিং শিক্ষাক্রম দলের একসাথে কাজ করার মাধ্যমে তৈরি করা হয়েছে। এই বিষয়বস্তু CC-BY-NC-SA দিয়ে লাইসেন্সকৃত।

আলোচনায় অংশ নিতে চাও?

কোন আলাপচারিতা নেই।
ইংরেজি জানো? খান একাডেমির ইংরেজি সাইটে আরো আলোচনা দেখতে এখানে ক্লিক কর।