সংখ্যা N কি দুটি বা অধিক কনsekutিভ নাম্বারগুলোর যোগফল হিসেবে লেখা যায়?
নિયম: N যদি ২-এর পাওয়ার (Power of 2) হয়, তবে কনsekutিভ যোগফল হিসেবে লেখা যায়না। অন্যথায় হয়।
কারণ
- দুটি কনsekutিভ সংখ্যার যোগফল বিজোড়।
- ২ⁿ = 2ⁿ⁻¹ + 2ⁿ⁻¹ (কনsekutিভ নয়)।
C++ কোড উদাহরণ
#include <iostream>
using namespace std;
bool isSumOfConsecutive(int n) {
return (n & (n-1)) && n; // n is NOT power of 2
}
int main() {
int n = 36;
cout << (isSumOfConsecutive(n) ? "Can be represented" : "Cannot be represented");
return 0;
}
আউটপুট
Can be represented